mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use EntityDescription - flunearyou (#55126)
This commit is contained in:
parent
c9e8d42405
commit
e99761fd7f
@ -1,7 +1,7 @@
|
|||||||
"""Support for user- and CDC-based flu info sensors from Flu Near You."""
|
"""Support for user- and CDC-based flu info sensors from Flu Near You."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
@ -38,20 +38,63 @@ SENSOR_TYPE_USER_NO_SYMPTOMS = "none"
|
|||||||
SENSOR_TYPE_USER_SYMPTOMS = "symptoms"
|
SENSOR_TYPE_USER_SYMPTOMS = "symptoms"
|
||||||
SENSOR_TYPE_USER_TOTAL = "total"
|
SENSOR_TYPE_USER_TOTAL = "total"
|
||||||
|
|
||||||
CDC_SENSORS = [
|
CDC_SENSOR_DESCRIPTIONS = (
|
||||||
(SENSOR_TYPE_CDC_LEVEL, "CDC Level", "mdi:biohazard", None),
|
SensorEntityDescription(
|
||||||
(SENSOR_TYPE_CDC_LEVEL2, "CDC Level 2", "mdi:biohazard", None),
|
key=SENSOR_TYPE_CDC_LEVEL,
|
||||||
]
|
name="CDC Level",
|
||||||
|
icon="mdi:biohazard",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_CDC_LEVEL2,
|
||||||
|
name="CDC Level 2",
|
||||||
|
icon="mdi:biohazard",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
USER_SENSORS = [
|
USER_SENSOR_DESCRIPTIONS = (
|
||||||
(SENSOR_TYPE_USER_CHICK, "Avian Flu Symptoms", "mdi:alert", "reports"),
|
SensorEntityDescription(
|
||||||
(SENSOR_TYPE_USER_DENGUE, "Dengue Fever Symptoms", "mdi:alert", "reports"),
|
key=SENSOR_TYPE_USER_CHICK,
|
||||||
(SENSOR_TYPE_USER_FLU, "Flu Symptoms", "mdi:alert", "reports"),
|
name="Avian Flu Symptoms",
|
||||||
(SENSOR_TYPE_USER_LEPTO, "Leptospirosis Symptoms", "mdi:alert", "reports"),
|
icon="mdi:alert",
|
||||||
(SENSOR_TYPE_USER_NO_SYMPTOMS, "No Symptoms", "mdi:alert", "reports"),
|
native_unit_of_measurement="reports",
|
||||||
(SENSOR_TYPE_USER_SYMPTOMS, "Flu-like Symptoms", "mdi:alert", "reports"),
|
),
|
||||||
(SENSOR_TYPE_USER_TOTAL, "Total Symptoms", "mdi:alert", "reports"),
|
SensorEntityDescription(
|
||||||
]
|
key=SENSOR_TYPE_USER_DENGUE,
|
||||||
|
name="Dengue Fever Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_USER_FLU,
|
||||||
|
name="Flu Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_USER_LEPTO,
|
||||||
|
name="Leptospirosis Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_USER_NO_SYMPTOMS,
|
||||||
|
name="No Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_USER_SYMPTOMS,
|
||||||
|
name="Flu-like Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_USER_TOTAL,
|
||||||
|
name="Total Symptoms",
|
||||||
|
icon="mdi:alert",
|
||||||
|
native_unit_of_measurement="reports",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
EXTENDED_SENSOR_TYPE_MAPPING = {
|
EXTENDED_SENSOR_TYPE_MAPPING = {
|
||||||
SENSOR_TYPE_USER_FLU: "ili",
|
SENSOR_TYPE_USER_FLU: "ili",
|
||||||
@ -66,32 +109,16 @@ async def async_setup_entry(
|
|||||||
"""Set up Flu Near You sensors based on a config entry."""
|
"""Set up Flu Near You sensors based on a config entry."""
|
||||||
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||||
|
|
||||||
sensors: list[CdcSensor | UserSensor] = []
|
sensors: list[CdcSensor | UserSensor] = [
|
||||||
|
CdcSensor(coordinators[CATEGORY_CDC_REPORT], entry, description)
|
||||||
for (sensor_type, name, icon, unit) in CDC_SENSORS:
|
for description in CDC_SENSOR_DESCRIPTIONS
|
||||||
sensors.append(
|
]
|
||||||
CdcSensor(
|
sensors.extend(
|
||||||
coordinators[CATEGORY_CDC_REPORT],
|
[
|
||||||
entry,
|
UserSensor(coordinators[CATEGORY_USER_REPORT], entry, description)
|
||||||
sensor_type,
|
for description in USER_SENSOR_DESCRIPTIONS
|
||||||
name,
|
]
|
||||||
icon,
|
)
|
||||||
unit,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
for (sensor_type, name, icon, unit) in USER_SENSORS:
|
|
||||||
sensors.append(
|
|
||||||
UserSensor(
|
|
||||||
coordinators[CATEGORY_USER_REPORT],
|
|
||||||
entry,
|
|
||||||
sensor_type,
|
|
||||||
name,
|
|
||||||
icon,
|
|
||||||
unit,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(sensors)
|
async_add_entities(sensors)
|
||||||
|
|
||||||
|
|
||||||
@ -102,23 +129,18 @@ class FluNearYouSensor(CoordinatorEntity, SensorEntity):
|
|||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator,
|
coordinator: DataUpdateCoordinator,
|
||||||
entry: ConfigEntry,
|
entry: ConfigEntry,
|
||||||
sensor_type: str,
|
description: SensorEntityDescription,
|
||||||
name: str,
|
|
||||||
icon: str,
|
|
||||||
unit: str | None,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
||||||
self._attr_icon = icon
|
|
||||||
self._attr_name = name
|
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"{entry.data[CONF_LATITUDE]},"
|
f"{entry.data[CONF_LATITUDE]},"
|
||||||
f"{entry.data[CONF_LONGITUDE]}_{sensor_type}"
|
f"{entry.data[CONF_LONGITUDE]}_{description.key}"
|
||||||
)
|
)
|
||||||
self._attr_native_unit_of_measurement = unit
|
|
||||||
self._entry = entry
|
self._entry = entry
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
@ -149,7 +171,7 @@ class CdcSensor(FluNearYouSensor):
|
|||||||
ATTR_STATE: self.coordinator.data["name"],
|
ATTR_STATE: self.coordinator.data["name"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self._attr_native_value = self.coordinator.data[self._sensor_type]
|
self._attr_native_value = self.coordinator.data[self.entity_description.key]
|
||||||
|
|
||||||
|
|
||||||
class UserSensor(FluNearYouSensor):
|
class UserSensor(FluNearYouSensor):
|
||||||
@ -168,10 +190,10 @@ class UserSensor(FluNearYouSensor):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
if self._sensor_type in self.coordinator.data["state"]["data"]:
|
if self.entity_description.key in self.coordinator.data["state"]["data"]:
|
||||||
states_key = self._sensor_type
|
states_key = self.entity_description.key
|
||||||
elif self._sensor_type in EXTENDED_SENSOR_TYPE_MAPPING:
|
elif self.entity_description.key in EXTENDED_SENSOR_TYPE_MAPPING:
|
||||||
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self._sensor_type]
|
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self.entity_description.key]
|
||||||
|
|
||||||
self._attr_extra_state_attributes[
|
self._attr_extra_state_attributes[
|
||||||
ATTR_STATE_REPORTS_THIS_WEEK
|
ATTR_STATE_REPORTS_THIS_WEEK
|
||||||
@ -180,7 +202,7 @@ class UserSensor(FluNearYouSensor):
|
|||||||
ATTR_STATE_REPORTS_LAST_WEEK
|
ATTR_STATE_REPORTS_LAST_WEEK
|
||||||
] = self.coordinator.data["state"]["last_week_data"][states_key]
|
] = self.coordinator.data["state"]["last_week_data"][states_key]
|
||||||
|
|
||||||
if self._sensor_type == SENSOR_TYPE_USER_TOTAL:
|
if self.entity_description.key == SENSOR_TYPE_USER_TOTAL:
|
||||||
self._attr_native_value = sum(
|
self._attr_native_value = sum(
|
||||||
v
|
v
|
||||||
for k, v in self.coordinator.data["local"].items()
|
for k, v in self.coordinator.data["local"].items()
|
||||||
@ -194,4 +216,6 @@ class UserSensor(FluNearYouSensor):
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = self.coordinator.data["local"][self._sensor_type]
|
self._attr_native_value = self.coordinator.data["local"][
|
||||||
|
self.entity_description.key
|
||||||
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user