add deprecation warning for task sensors

This commit is contained in:
tr4nt0r
2024-04-26 21:03:32 +00:00
parent 8ae07a4786
commit 9e58053f3b
3 changed files with 92 additions and 2 deletions

View File

@@ -10,6 +10,9 @@
"0": "mdi:skull-outline"
}
},
"health_max": {
"default": "mdi:heart"
},
"mana": {
"default": "mdi:flask",
"state": {

View File

@@ -19,6 +19,11 @@ from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import (
IssueSeverity,
async_create_issue,
async_delete_issue,
)
from .const import DOMAIN, MANUFACTURER, NAME
@@ -41,6 +46,7 @@ class HabitipySensorEntity(StrEnum):
DISPLAY_NAME = "display_name"
HEALTH = "health"
HEALTH_MAX = "health_max"
MANA = "mana"
MANA_MAX = "mana_max"
EXPERIENCE = "experience"
@@ -63,6 +69,13 @@ SENSOR_DESCRIPTIONS: dict[str, HabitipySensorEntityDescription] = {
suggested_display_precision=0,
value_path=["stats", "hp"],
),
HabitipySensorEntity.HEALTH_MAX: HabitipySensorEntityDescription(
key=HabitipySensorEntity.HEALTH_MAX,
translation_key=HabitipySensorEntity.HEALTH_MAX,
native_unit_of_measurement="HP",
entity_registry_enabled_default=False,
value_path=["stats", "maxHealth"],
),
HabitipySensorEntity.MANA: HabitipySensorEntityDescription(
key=HabitipySensorEntity.MANA,
translation_key=HabitipySensorEntity.MANA,
@@ -165,8 +178,10 @@ async def async_setup_entry(
HabitipySensor(sensor_data, description, config_entry)
for description in SENSOR_DESCRIPTIONS.values()
]
# Task sensors are deprecated and will be removed in 2024.12
entities.extend(
HabitipyTaskSensor(name, task_type, sensor_data) for task_type in TASKS_TYPES
HabitipyTaskSensor(hass, name, task_type, sensor_data, config_entry)
for task_type in TASKS_TYPES
)
async_add_entities(entities, True)
@@ -207,17 +222,53 @@ class HabitipySensor(SensorEntity):
data = data[element]
self._attr_native_value = data
async def async_added_to_hass(self) -> None:
"""Raise issue when entity is registered and was not disabled."""
if self.entity_description.key == HabitipySensorEntity.HEALTH_MAX:
if self.enabled:
name = self.name
async_create_issue(
self.hass,
DOMAIN,
f"deprecated_sensor_entity_{self.entity_description.key}",
breaks_in_ha_version="2024.12.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_sensor_entity",
translation_placeholders={
"entity_name": str(name),
"entity": self.entity_id,
},
)
else:
async_delete_issue(
self.hass,
DOMAIN,
f"deprecated_sensor_entity_{self.entity_description.key}",
)
# Task sensors are deprecated and will be removed in 2024.12
class HabitipyTaskSensor(SensorEntity):
"""A Habitica task sensor."""
def __init__(self, name, task_name, updater):
def __init__(self, hass, name, task_name, updater, entry):
"""Initialize a generic Habitica task."""
# self.hass = hass
self._name = name
self._task_name = task_name
self._task_type = TASKS_TYPES[task_name]
self._state = None
self._updater = updater
self._attr_unique_id = f"{entry.unique_id}_{task_name}"
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
manufacturer=MANUFACTURER,
model=NAME,
name=entry.data[CONF_NAME],
configuration_url=entry.data[CONF_URL],
identifiers={(DOMAIN, entry.unique_id)},
)
async def async_update(self) -> None:
"""Update Condition and Forecast."""
@@ -227,6 +278,29 @@ class HabitipyTaskSensor(SensorEntity):
tasks_length = len(all_tasks[element])
self._state = tasks_length
async def async_added_to_hass(self) -> None:
"""Raise issue when entity is registered and was not disabled."""
if self.enabled:
async_create_issue(
self.hass,
DOMAIN,
f"deprecated_task_entity_{self._task_name}",
breaks_in_ha_version="2024.12.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_task_entity",
translation_placeholders={
"task_name": self._task_name,
"entity": f"sensor.habitica_{self._name}_{self._task_name}",
},
)
else:
async_delete_issue(
self.hass,
DOMAIN,
f"deprecated_task_entity_{self._task_name}",
)
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""

View File

@@ -27,6 +27,9 @@
"health": {
"name": "Health"
},
"health_max": {
"name": "Health max."
},
"mana": {
"name": "Mana"
},
@@ -56,6 +59,16 @@
}
}
},
"issues": {
"deprecated_task_entity": {
"title": "The Habitica `{task_name}` sensor is deprecated",
"description": "The Habitica entity `{entity}` is deprecated and will be removed in `2024.12`.\nPlease update your automations and scripts to replace the sensor entity with the new todo entity.\nWhen you are done migrating you can disable the entity."
},
"deprecated_sensor_entity": {
"title": "The Habitica `{entity_name}` sensor is deprecated",
"description": "The Habitica entity `{entity}` is deprecated and will be removed in `2024.12`.\nEach player can have a maximum of 50 HP, and this maximum cannot be altered, thus the `{entity_name}` sensor serves no purpose.\nYou can disable the entity."
}
},
"services": {
"api_call": {
"name": "API name",