2019-04-03 15:40:03 +00:00
|
|
|
"""Support for user- and CDC-based flu info sensors from Flu Near You."""
|
2021-07-27 08:42:51 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-08-25 08:52:37 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2021-07-27 08:42:51 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-11-08 02:45:14 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
ATTR_STATE,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
2020-04-02 23:54:11 +00:00
|
|
|
)
|
2021-07-27 08:42:51 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2020-11-08 02:45:14 +00:00
|
|
|
|
|
|
|
from .const import CATEGORY_CDC_REPORT, CATEGORY_USER_REPORT, DATA_COORDINATOR, DOMAIN
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CITY = "city"
|
|
|
|
ATTR_REPORTED_DATE = "reported_date"
|
|
|
|
ATTR_REPORTED_LATITUDE = "reported_latitude"
|
|
|
|
ATTR_REPORTED_LONGITUDE = "reported_longitude"
|
|
|
|
ATTR_STATE_REPORTS_LAST_WEEK = "state_reports_last_week"
|
|
|
|
ATTR_STATE_REPORTS_THIS_WEEK = "state_reports_this_week"
|
|
|
|
ATTR_ZIP_CODE = "zip_code"
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_ATTRIBUTION = "Data provided by Flu Near You"
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
SENSOR_TYPE_CDC_LEVEL = "level"
|
|
|
|
SENSOR_TYPE_CDC_LEVEL2 = "level2"
|
|
|
|
SENSOR_TYPE_USER_CHICK = "chick"
|
|
|
|
SENSOR_TYPE_USER_DENGUE = "dengue"
|
|
|
|
SENSOR_TYPE_USER_FLU = "flu"
|
|
|
|
SENSOR_TYPE_USER_LEPTO = "lepto"
|
|
|
|
SENSOR_TYPE_USER_NO_SYMPTOMS = "none"
|
|
|
|
SENSOR_TYPE_USER_SYMPTOMS = "symptoms"
|
|
|
|
SENSOR_TYPE_USER_TOTAL = "total"
|
|
|
|
|
2021-08-25 08:52:37 +00:00
|
|
|
CDC_SENSOR_DESCRIPTIONS = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
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_SENSOR_DESCRIPTIONS = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_USER_CHICK,
|
|
|
|
name="Avian Flu Symptoms",
|
|
|
|
icon="mdi:alert",
|
|
|
|
native_unit_of_measurement="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",
|
|
|
|
),
|
|
|
|
)
|
2020-11-08 02:45:14 +00:00
|
|
|
|
|
|
|
EXTENDED_SENSOR_TYPE_MAPPING = {
|
|
|
|
SENSOR_TYPE_USER_FLU: "ili",
|
|
|
|
SENSOR_TYPE_USER_NO_SYMPTOMS: "no_symptoms",
|
|
|
|
SENSOR_TYPE_USER_TOTAL: "total_surveys",
|
2018-12-17 01:51:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2021-07-27 08:42:51 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2020-04-02 23:54:11 +00:00
|
|
|
"""Set up Flu Near You sensors based on a config entry."""
|
2021-07-01 09:04:39 +00:00
|
|
|
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
2020-11-08 02:45:14 +00:00
|
|
|
|
2021-08-25 08:52:37 +00:00
|
|
|
sensors: list[CdcSensor | UserSensor] = [
|
|
|
|
CdcSensor(coordinators[CATEGORY_CDC_REPORT], entry, description)
|
|
|
|
for description in CDC_SENSOR_DESCRIPTIONS
|
|
|
|
]
|
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
UserSensor(coordinators[CATEGORY_USER_REPORT], entry, description)
|
|
|
|
for description in USER_SENSOR_DESCRIPTIONS
|
|
|
|
]
|
|
|
|
)
|
2020-11-08 02:45:14 +00:00
|
|
|
async_add_entities(sensors)
|
2018-11-09 15:23:07 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class FluNearYouSensor(CoordinatorEntity, SensorEntity):
|
2018-11-09 15:23:07 +00:00
|
|
|
"""Define a base Flu Near You sensor."""
|
|
|
|
|
2021-07-27 08:42:51 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
entry: ConfigEntry,
|
2021-08-25 08:52:37 +00:00
|
|
|
description: SensorEntityDescription,
|
2021-07-27 08:42:51 +00:00
|
|
|
) -> None:
|
2018-11-09 15:23:07 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-11-08 02:45:14 +00:00
|
|
|
super().__init__(coordinator)
|
2021-08-25 08:52:37 +00:00
|
|
|
|
2021-07-01 09:04:39 +00:00
|
|
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{entry.data[CONF_LATITUDE]},"
|
2021-08-25 08:52:37 +00:00
|
|
|
f"{entry.data[CONF_LONGITUDE]}_{description.key}"
|
2020-11-08 02:45:14 +00:00
|
|
|
)
|
2021-07-01 09:04:39 +00:00
|
|
|
self._entry = entry
|
2021-08-25 08:52:37 +00:00
|
|
|
self.entity_description = description
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
self.update_from_latest_data()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
2021-07-27 08:42:51 +00:00
|
|
|
async def async_added_to_hass(self) -> None:
|
2020-04-02 23:54:11 +00:00
|
|
|
"""Register callbacks."""
|
2020-11-08 02:45:14 +00:00
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.update_from_latest_data()
|
2020-04-02 23:54:11 +00:00
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
@callback
|
2021-07-27 08:42:51 +00:00
|
|
|
def update_from_latest_data(self) -> None:
|
2020-11-08 02:45:14 +00:00
|
|
|
"""Update the sensor."""
|
|
|
|
raise NotImplementedError
|
2020-04-02 23:54:11 +00:00
|
|
|
|
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
class CdcSensor(FluNearYouSensor):
|
|
|
|
"""Define a sensor for CDC reports."""
|
2020-04-02 23:54:11 +00:00
|
|
|
|
|
|
|
@callback
|
2021-07-27 08:42:51 +00:00
|
|
|
def update_from_latest_data(self) -> None:
|
2020-04-02 23:54:11 +00:00
|
|
|
"""Update the sensor."""
|
2021-07-01 09:04:39 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2020-11-08 02:45:14 +00:00
|
|
|
{
|
|
|
|
ATTR_REPORTED_DATE: self.coordinator.data["week_date"],
|
|
|
|
ATTR_STATE: self.coordinator.data["name"],
|
|
|
|
}
|
|
|
|
)
|
2021-08-25 08:52:37 +00:00
|
|
|
self._attr_native_value = self.coordinator.data[self.entity_description.key]
|
2020-11-08 02:45:14 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
class UserSensor(FluNearYouSensor):
|
|
|
|
"""Define a sensor for user reports."""
|
|
|
|
|
|
|
|
@callback
|
2021-07-27 08:42:51 +00:00
|
|
|
def update_from_latest_data(self) -> None:
|
2020-11-08 02:45:14 +00:00
|
|
|
"""Update the sensor."""
|
2021-07-01 09:04:39 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2020-11-08 02:45:14 +00:00
|
|
|
{
|
|
|
|
ATTR_CITY: self.coordinator.data["local"]["city"].split("(")[0],
|
|
|
|
ATTR_REPORTED_LATITUDE: self.coordinator.data["local"]["latitude"],
|
|
|
|
ATTR_REPORTED_LONGITUDE: self.coordinator.data["local"]["longitude"],
|
|
|
|
ATTR_STATE: self.coordinator.data["state"]["name"],
|
|
|
|
ATTR_ZIP_CODE: self.coordinator.data["local"]["zip"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-08-25 08:52:37 +00:00
|
|
|
if self.entity_description.key in self.coordinator.data["state"]["data"]:
|
|
|
|
states_key = self.entity_description.key
|
|
|
|
elif self.entity_description.key in EXTENDED_SENSOR_TYPE_MAPPING:
|
|
|
|
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self.entity_description.key]
|
2020-11-08 02:45:14 +00:00
|
|
|
|
2021-07-01 09:04:39 +00:00
|
|
|
self._attr_extra_state_attributes[
|
|
|
|
ATTR_STATE_REPORTS_THIS_WEEK
|
|
|
|
] = self.coordinator.data["state"]["data"][states_key]
|
|
|
|
self._attr_extra_state_attributes[
|
|
|
|
ATTR_STATE_REPORTS_LAST_WEEK
|
|
|
|
] = self.coordinator.data["state"]["last_week_data"][states_key]
|
2020-11-08 02:45:14 +00:00
|
|
|
|
2021-08-25 08:52:37 +00:00
|
|
|
if self.entity_description.key == SENSOR_TYPE_USER_TOTAL:
|
2021-08-12 12:23:56 +00:00
|
|
|
self._attr_native_value = sum(
|
2020-11-08 02:45:14 +00:00
|
|
|
v
|
|
|
|
for k, v in self.coordinator.data["local"].items()
|
|
|
|
if k
|
|
|
|
in (
|
|
|
|
SENSOR_TYPE_USER_CHICK,
|
|
|
|
SENSOR_TYPE_USER_DENGUE,
|
|
|
|
SENSOR_TYPE_USER_FLU,
|
|
|
|
SENSOR_TYPE_USER_LEPTO,
|
|
|
|
SENSOR_TYPE_USER_SYMPTOMS,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-11-08 02:45:14 +00:00
|
|
|
)
|
|
|
|
else:
|
2021-08-25 08:52:37 +00:00
|
|
|
self._attr_native_value = self.coordinator.data["local"][
|
|
|
|
self.entity_description.key
|
|
|
|
]
|