2019-04-03 15:40:03 +00:00
|
|
|
"""Support for user- and CDC-based flu info sensors from Flu Near You."""
|
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
|
|
|
)
|
2020-11-08 02:45:14 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
CDC_SENSORS = [
|
|
|
|
(SENSOR_TYPE_CDC_LEVEL, "CDC Level", "mdi:biohazard", None),
|
|
|
|
(SENSOR_TYPE_CDC_LEVEL2, "CDC Level 2", "mdi:biohazard", None),
|
|
|
|
]
|
|
|
|
|
|
|
|
USER_SENSORS = [
|
|
|
|
(SENSOR_TYPE_USER_CHICK, "Avian Flu Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_DENGUE, "Dengue Fever Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_FLU, "Flu Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_LEPTO, "Leptospirosis Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_NO_SYMPTOMS, "No Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_SYMPTOMS, "Flu-like Symptoms", "mdi:alert", "reports"),
|
|
|
|
(SENSOR_TYPE_USER_TOTAL, "Total Symptoms", "mdi:alert", "reports"),
|
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-04-02 23:54:11 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up Flu Near You sensors based on a config entry."""
|
2020-11-08 02:45:14 +00:00
|
|
|
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][config_entry.entry_id]
|
|
|
|
|
|
|
|
sensors = []
|
|
|
|
|
|
|
|
for (sensor_type, name, icon, unit) in CDC_SENSORS:
|
|
|
|
sensors.append(
|
|
|
|
CdcSensor(
|
|
|
|
coordinators[CATEGORY_CDC_REPORT],
|
|
|
|
config_entry,
|
|
|
|
sensor_type,
|
|
|
|
name,
|
|
|
|
icon,
|
|
|
|
unit,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for (sensor_type, name, icon, unit) in USER_SENSORS:
|
|
|
|
sensors.append(
|
|
|
|
UserSensor(
|
|
|
|
coordinators[CATEGORY_USER_REPORT],
|
|
|
|
config_entry,
|
|
|
|
sensor_type,
|
|
|
|
name,
|
|
|
|
icon,
|
|
|
|
unit,
|
|
|
|
)
|
|
|
|
)
|
2018-11-09 15:23:07 +00:00
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
async_add_entities(sensors)
|
2018-11-09 15:23:07 +00:00
|
|
|
|
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
class FluNearYouSensor(CoordinatorEntity):
|
2018-11-09 15:23:07 +00:00
|
|
|
"""Define a base Flu Near You sensor."""
|
|
|
|
|
2020-11-08 02:45:14 +00:00
|
|
|
def __init__(self, coordinator, config_entry, sensor_type, name, icon, unit):
|
2018-11-09 15:23:07 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-11-08 02:45:14 +00:00
|
|
|
super().__init__(coordinator)
|
2018-11-09 15:23:07 +00:00
|
|
|
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
2020-11-08 02:45:14 +00:00
|
|
|
self._config_entry = config_entry
|
2018-11-09 15:23:07 +00:00
|
|
|
self._icon = icon
|
|
|
|
self._name = name
|
2020-04-02 23:54:11 +00:00
|
|
|
self._sensor_type = sensor_type
|
2018-11-09 15:23:07 +00:00
|
|
|
self._state = None
|
|
|
|
self._unit = unit
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2018-11-09 15:23:07 +00:00
|
|
|
"""Return the device state attributes."""
|
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2020-11-08 02:45:14 +00:00
|
|
|
return (
|
|
|
|
f"{self._config_entry.data[CONF_LATITUDE]},"
|
|
|
|
f"{self._config_entry.data[CONF_LONGITUDE]}_{self._sensor_type}"
|
|
|
|
)
|
2018-11-09 15:23:07 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
return self._unit
|
|
|
|
|
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()
|
|
|
|
|
2020-04-02 23:54:11 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""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
|
|
|
|
def update_from_latest_data(self):
|
|
|
|
"""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
|
|
|
|
def update_from_latest_data(self):
|
|
|
|
"""Update the sensor."""
|
2020-11-08 02:45:14 +00:00
|
|
|
self._attrs.update(
|
|
|
|
{
|
|
|
|
ATTR_REPORTED_DATE: self.coordinator.data["week_date"],
|
|
|
|
ATTR_STATE: self.coordinator.data["name"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self._state = self.coordinator.data[self._sensor_type]
|
|
|
|
|
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
|
|
|
|
def update_from_latest_data(self):
|
|
|
|
"""Update the sensor."""
|
|
|
|
self._attrs.update(
|
|
|
|
{
|
|
|
|
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"],
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
if self._sensor_type in self.coordinator.data["state"]["data"]:
|
|
|
|
states_key = self._sensor_type
|
|
|
|
elif self._sensor_type in EXTENDED_SENSOR_TYPE_MAPPING:
|
|
|
|
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self._sensor_type]
|
|
|
|
|
|
|
|
self._attrs[ATTR_STATE_REPORTS_THIS_WEEK] = self.coordinator.data["state"][
|
|
|
|
"data"
|
|
|
|
][states_key]
|
|
|
|
self._attrs[ATTR_STATE_REPORTS_LAST_WEEK] = self.coordinator.data["state"][
|
|
|
|
"last_week_data"
|
|
|
|
][states_key]
|
|
|
|
|
|
|
|
if self._sensor_type == SENSOR_TYPE_USER_TOTAL:
|
|
|
|
self._state = sum(
|
|
|
|
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:
|
|
|
|
self._state = self.coordinator.data["local"][self._sensor_type]
|