Skip to content

Commit b246f0c

Browse files
committed
move to setup, remove disabled
1 parent 944c608 commit b246f0c

File tree

3 files changed

+176
-136
lines changed

3 files changed

+176
-136
lines changed

homeassistant/components/habitica/sensor.py

Lines changed: 96 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,102 @@ async def async_setup_entry(
261261
"""Set up the habitica sensors."""
262262

263263
coordinator = config_entry.runtime_data
264+
ent_reg = er.async_get(hass)
265+
entities: list[SensorEntity] = []
266+
description: SensorEntityDescription
267+
for description in SENSOR_DESCRIPTIONS:
268+
if description.key is HabitipySensorEntity.HEALTH_MAX:
269+
if entity_id := ent_reg.async_get_entity_id(
270+
SENSOR_DOMAIN,
271+
DOMAIN,
272+
f"{config_entry.unique_id}_{description.key}",
273+
):
274+
entity_entry = ent_reg.async_get(entity_id)
275+
assert entity_entry
276+
if entity_entry.disabled:
277+
ent_reg.async_remove(entity_id)
278+
async_delete_issue(
279+
hass,
280+
DOMAIN,
281+
f"deprecated_entity_{description.key}",
282+
)
283+
else:
284+
entities.append(HabitipySensor(coordinator, description))
285+
if entity_used_in(hass, entity_id):
286+
async_create_issue(
287+
hass,
288+
DOMAIN,
289+
f"deprecated_entity_{description.key}",
290+
breaks_in_ha_version="2025.8.0",
291+
is_fixable=False,
292+
severity=IssueSeverity.WARNING,
293+
translation_key="deprecated_entity",
294+
translation_placeholders={
295+
"name": str(
296+
entity_entry.name or entity_entry.original_name
297+
),
298+
"entity": entity_id,
299+
},
300+
)
301+
else:
302+
entities.append(HabitipySensor(coordinator, description))
303+
304+
for description in TASK_SENSOR_DESCRIPTION:
305+
if entity_id := ent_reg.async_get_entity_id(
306+
SENSOR_DOMAIN,
307+
DOMAIN,
308+
f"{config_entry.unique_id}_{description.key}",
309+
):
310+
entity_entry = ent_reg.async_get(entity_id)
311+
assert entity_entry
312+
if entity_entry.disabled:
313+
ent_reg.async_remove(entity_id)
314+
async_delete_issue(
315+
hass,
316+
DOMAIN,
317+
f"deprecated_task_entity_{description.key}",
318+
)
319+
else:
320+
entities.append(HabitipyTaskSensor(coordinator, description))
321+
if description.key in (
322+
HabitipySensorEntity.TODOS,
323+
HabitipySensorEntity.DAILIES,
324+
) and entity_used_in(hass, entity_id):
325+
async_create_issue(
326+
hass,
327+
DOMAIN,
328+
f"deprecated_task_entity_{description.key}",
329+
breaks_in_ha_version="2025.2.0",
330+
is_fixable=False,
331+
severity=IssueSeverity.WARNING,
332+
translation_key="deprecated_task_entity",
333+
translation_placeholders={
334+
"task_name": str(
335+
entity_entry.name or entity_entry.original_name
336+
),
337+
"entity": entity_id,
338+
},
339+
)
340+
elif description.key in (
341+
HabitipySensorEntity.REWARDS,
342+
HabitipySensorEntity.HABITS,
343+
) and entity_used_in(hass, entity_id):
344+
async_create_issue(
345+
hass,
346+
DOMAIN,
347+
f"deprecated_task_entity_{description.key}",
348+
breaks_in_ha_version="2025.8.0",
349+
is_fixable=False,
350+
severity=IssueSeverity.WARNING,
351+
translation_key="deprecated_entity",
352+
translation_placeholders={
353+
"name": str(
354+
entity_entry.name or entity_entry.original_name
355+
),
356+
"entity": entity_id,
357+
},
358+
)
264359

265-
entities: list[SensorEntity] = [
266-
HabitipySensor(coordinator, description) for description in SENSOR_DESCRIPTIONS
267-
]
268-
entities.extend(
269-
HabitipyTaskSensor(coordinator, description)
270-
for description in TASK_SENSOR_DESCRIPTION
271-
)
272360
async_add_entities(entities, True)
273361

274362

@@ -303,33 +391,7 @@ async def async_added_to_hass(self) -> None:
303391
"""Raise issue when entity is registered and was not disabled."""
304392
if TYPE_CHECKING:
305393
assert self.unique_id
306-
if entity_id := er.async_get(self.hass).async_get_entity_id(
307-
SENSOR_DOMAIN, DOMAIN, self.unique_id
308-
):
309-
if (
310-
self.enabled
311-
and self.entity_description.key is HabitipySensorEntity.HEALTH_MAX
312-
and entity_used_in(self.hass, entity_id)
313-
):
314-
async_create_issue(
315-
self.hass,
316-
DOMAIN,
317-
f"deprecated_entity_{self.entity_description.key}",
318-
breaks_in_ha_version="2025.8.0",
319-
is_fixable=False,
320-
severity=IssueSeverity.WARNING,
321-
translation_key="deprecated_entity",
322-
translation_placeholders={
323-
"name": str(self.name),
324-
"entity": entity_id,
325-
},
326-
)
327-
else:
328-
async_delete_issue(
329-
self.hass,
330-
DOMAIN,
331-
f"deprecated_entity_{self.entity_description.key}",
332-
)
394+
333395
await super().async_added_to_hass()
334396

335397

@@ -359,56 +421,3 @@ def extra_state_attributes(self) -> Mapping[str, Any] | None:
359421
task[map_key] = value
360422
attrs[str(task_id)] = task
361423
return attrs
362-
363-
async def async_added_to_hass(self) -> None:
364-
"""Raise issue when entity is registered and was not disabled."""
365-
if TYPE_CHECKING:
366-
assert self.unique_id
367-
if entity_id := er.async_get(self.hass).async_get_entity_id(
368-
SENSOR_DOMAIN, DOMAIN, self.unique_id
369-
):
370-
if (
371-
self.enabled
372-
and self.entity_description.key
373-
in (HabitipySensorEntity.TODOS, HabitipySensorEntity.DAILIES)
374-
and entity_used_in(self.hass, entity_id)
375-
):
376-
async_create_issue(
377-
self.hass,
378-
DOMAIN,
379-
f"deprecated_task_entity_{self.entity_description.key}",
380-
breaks_in_ha_version="2025.2.0",
381-
is_fixable=False,
382-
severity=IssueSeverity.WARNING,
383-
translation_key="deprecated_task_entity",
384-
translation_placeholders={
385-
"task_name": str(self.name),
386-
"entity": entity_id,
387-
},
388-
)
389-
elif (
390-
self.enabled
391-
and self.entity_description.key
392-
in (HabitipySensorEntity.REWARDS, HabitipySensorEntity.HABITS)
393-
and entity_used_in(self.hass, entity_id)
394-
):
395-
async_create_issue(
396-
self.hass,
397-
DOMAIN,
398-
f"deprecated_task_entity_{self.entity_description.key}",
399-
breaks_in_ha_version="2025.8.0",
400-
is_fixable=False,
401-
severity=IssueSeverity.WARNING,
402-
translation_key="deprecated_entity",
403-
translation_placeholders={
404-
"name": str(self.name),
405-
"entity": entity_id,
406-
},
407-
)
408-
else:
409-
async_delete_issue(
410-
self.hass,
411-
DOMAIN,
412-
f"deprecated_task_entity_{self.entity_description.key}",
413-
)
414-
await super().async_added_to_hass()

tests/components/habitica/snapshots/test_sensor.ambr

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,53 @@
826826
'state': '0.0',
827827
})
828828
# ---
829+
# name: test_sensors[sensor.test_user_health_max-entry]
830+
EntityRegistryEntrySnapshot({
831+
'aliases': set({
832+
}),
833+
'area_id': None,
834+
'capabilities': None,
835+
'config_entry_id': <ANY>,
836+
'device_class': None,
837+
'device_id': <ANY>,
838+
'disabled_by': None,
839+
'domain': 'sensor',
840+
'entity_category': None,
841+
'entity_id': 'sensor.test_user_health_max',
842+
'has_entity_name': True,
843+
'hidden_by': None,
844+
'icon': None,
845+
'id': <ANY>,
846+
'labels': set({
847+
}),
848+
'name': None,
849+
'options': dict({
850+
}),
851+
'original_device_class': None,
852+
'original_icon': None,
853+
'original_name': 'Max. health',
854+
'platform': 'habitica',
855+
'previous_unique_id': None,
856+
'supported_features': 0,
857+
'translation_key': <HabitipySensorEntity.HEALTH_MAX: 'health_max'>,
858+
'unique_id': 'a380546a-94be-4b8e-8a0b-23e0d5c03303_health_max',
859+
'unit_of_measurement': 'HP',
860+
})
861+
# ---
862+
# name: test_sensors[sensor.test_user_health_max-state]
863+
StateSnapshot({
864+
'attributes': ReadOnlyDict({
865+
'friendly_name': 'test-user Max. health',
866+
'unit_of_measurement': 'HP',
867+
}),
868+
'context': <ANY>,
869+
'entity_id': 'sensor.test_user_health_max',
870+
'last_changed': <ANY>,
871+
'last_reported': <ANY>,
872+
'last_updated': <ANY>,
873+
'state': '50',
874+
})
875+
# ---
829876
# name: test_sensors[sensor.test_user_intelligence-entry]
830877
EntityRegistryEntrySnapshot({
831878
'aliases': set({
@@ -977,53 +1024,6 @@
9771024
'state': '50.9',
9781025
})
9791026
# ---
980-
# name: test_sensors[sensor.test_user_max_health-entry]
981-
EntityRegistryEntrySnapshot({
982-
'aliases': set({
983-
}),
984-
'area_id': None,
985-
'capabilities': None,
986-
'config_entry_id': <ANY>,
987-
'device_class': None,
988-
'device_id': <ANY>,
989-
'disabled_by': None,
990-
'domain': 'sensor',
991-
'entity_category': None,
992-
'entity_id': 'sensor.test_user_max_health',
993-
'has_entity_name': True,
994-
'hidden_by': None,
995-
'icon': None,
996-
'id': <ANY>,
997-
'labels': set({
998-
}),
999-
'name': None,
1000-
'options': dict({
1001-
}),
1002-
'original_device_class': None,
1003-
'original_icon': None,
1004-
'original_name': 'Max. health',
1005-
'platform': 'habitica',
1006-
'previous_unique_id': None,
1007-
'supported_features': 0,
1008-
'translation_key': <HabitipySensorEntity.HEALTH_MAX: 'health_max'>,
1009-
'unique_id': 'a380546a-94be-4b8e-8a0b-23e0d5c03303_health_max',
1010-
'unit_of_measurement': 'HP',
1011-
})
1012-
# ---
1013-
# name: test_sensors[sensor.test_user_max_health-state]
1014-
StateSnapshot({
1015-
'attributes': ReadOnlyDict({
1016-
'friendly_name': 'test-user Max. health',
1017-
'unit_of_measurement': 'HP',
1018-
}),
1019-
'context': <ANY>,
1020-
'entity_id': 'sensor.test_user_max_health',
1021-
'last_changed': <ANY>,
1022-
'last_reported': <ANY>,
1023-
'last_updated': <ANY>,
1024-
'state': '50',
1025-
})
1026-
# ---
10271027
# name: test_sensors[sensor.test_user_max_mana-entry]
10281028
EntityRegistryEntrySnapshot({
10291029
'aliases': set({

tests/components/habitica/test_sensor.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from homeassistant.components.habitica.const import DOMAIN
1010
from homeassistant.components.habitica.sensor import HabitipySensorEntity
11+
from homeassistant.components.sensor.const import DOMAIN as SENSOR_DOMAIN
1112
from homeassistant.config_entries import ConfigEntryState
1213
from homeassistant.const import Platform
1314
from homeassistant.core import HomeAssistant
@@ -26,7 +27,33 @@ def sensor_only() -> Generator[None]:
2627
yield
2728

2829

29-
@pytest.mark.usefixtures("habitica", "entity_registry_enabled_by_default")
30+
@pytest.fixture
31+
def inject_deprecated_entities(
32+
hass: HomeAssistant,
33+
entity_registry: er.EntityRegistry,
34+
) -> Generator[None]:
35+
"""Inject deprecated entities to entity registry."""
36+
37+
entity_registry = er.async_get(hass)
38+
for entity in (
39+
("test_user_dailies", "dailys"),
40+
("test_user_to_do_s", "todos"),
41+
("test_user_habits", "habits"),
42+
("test_user_rewards", "rewards"),
43+
("test_user_health_max", "health_max"),
44+
):
45+
entity_registry.async_get_or_create(
46+
SENSOR_DOMAIN,
47+
DOMAIN,
48+
f"a380546a-94be-4b8e-8a0b-23e0d5c03303_{entity[1]}",
49+
suggested_object_id=entity[0],
50+
disabled_by=None,
51+
)
52+
53+
54+
@pytest.mark.usefixtures(
55+
"habitica", "entity_registry_enabled_by_default", "inject_deprecated_entities"
56+
)
3057
async def test_sensors(
3158
hass: HomeAssistant,
3259
config_entry: MockConfigEntry,
@@ -44,14 +71,18 @@ async def test_sensors(
4471
await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)
4572

4673

47-
@pytest.mark.usefixtures("habitica", "entity_registry_enabled_by_default")
74+
@pytest.mark.usefixtures(
75+
"habitica", "entity_registry_enabled_by_default", "inject_deprecated_entities"
76+
)
4877
async def test_sensor_deprecation_issue(
4978
hass: HomeAssistant,
5079
config_entry: MockConfigEntry,
5180
issue_registry: ir.IssueRegistry,
81+
entity_registry: er.EntityRegistry,
5282
) -> None:
5383
"""Test task sensor deprecation issue."""
5484

85+
assert entity_registry is not None
5586
with patch(
5687
"homeassistant.components.habitica.sensor.entity_used_in", return_value=True
5788
):

0 commit comments

Comments
 (0)