Remove redundant property definitions in Flu Near You (#52377)
This commit is contained in:
parent
8f70b5c183
commit
7bd8e2aa55
2 changed files with 26 additions and 61 deletions
|
@ -25,14 +25,9 @@ CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
||||||
PLATFORMS = ["sensor"]
|
PLATFORMS = ["sensor"]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
|
||||||
"""Set up the Flu Near You component."""
|
|
||||||
hass.data[DOMAIN] = {DATA_COORDINATOR: {}}
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry):
|
async def async_setup_entry(hass, entry):
|
||||||
"""Set up Flu Near You as config entry."""
|
"""Set up Flu Near You as config entry."""
|
||||||
|
hass.data.setdefault(DOMAIN, {DATA_COORDINATOR: {}})
|
||||||
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = {}
|
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = {}
|
||||||
|
|
||||||
websession = aiohttp_client.async_get_clientsession(hass)
|
websession = aiohttp_client.async_get_clientsession(hass)
|
||||||
|
|
|
@ -53,9 +53,9 @@ EXTENDED_SENSOR_TYPE_MAPPING = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, entry, async_add_entities):
|
||||||
"""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][config_entry.entry_id]
|
coordinators = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
|
|
||||||
|
@ -63,7 +63,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
sensors.append(
|
sensors.append(
|
||||||
CdcSensor(
|
CdcSensor(
|
||||||
coordinators[CATEGORY_CDC_REPORT],
|
coordinators[CATEGORY_CDC_REPORT],
|
||||||
config_entry,
|
entry,
|
||||||
sensor_type,
|
sensor_type,
|
||||||
name,
|
name,
|
||||||
icon,
|
icon,
|
||||||
|
@ -75,7 +75,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
sensors.append(
|
sensors.append(
|
||||||
UserSensor(
|
UserSensor(
|
||||||
coordinators[CATEGORY_USER_REPORT],
|
coordinators[CATEGORY_USER_REPORT],
|
||||||
config_entry,
|
entry,
|
||||||
sensor_type,
|
sensor_type,
|
||||||
name,
|
name,
|
||||||
icon,
|
icon,
|
||||||
|
@ -89,49 +89,19 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
class FluNearYouSensor(CoordinatorEntity, SensorEntity):
|
class FluNearYouSensor(CoordinatorEntity, SensorEntity):
|
||||||
"""Define a base Flu Near You sensor."""
|
"""Define a base Flu Near You sensor."""
|
||||||
|
|
||||||
def __init__(self, coordinator, config_entry, sensor_type, name, icon, unit):
|
def __init__(self, coordinator, entry, sensor_type, name, icon, unit):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
||||||
self._config_entry = config_entry
|
self._attr_icon = icon
|
||||||
self._icon = icon
|
self._attr_name = name
|
||||||
self._name = name
|
self._attr_unique_id = (
|
||||||
self._sensor_type = sensor_type
|
f"{entry.data[CONF_LATITUDE]},"
|
||||||
self._state = None
|
f"{entry.data[CONF_LONGITUDE]}_{sensor_type}"
|
||||||
self._unit = unit
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""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):
|
|
||||||
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
|
||||||
return (
|
|
||||||
f"{self._config_entry.data[CONF_LATITUDE]},"
|
|
||||||
f"{self._config_entry.data[CONF_LONGITUDE]}_{self._sensor_type}"
|
|
||||||
)
|
)
|
||||||
|
self._attr_unit_of_measurement = unit
|
||||||
@property
|
self._entry = entry
|
||||||
def unit_of_measurement(self):
|
self._sensor_type = sensor_type
|
||||||
"""Return the unit the value is expressed in."""
|
|
||||||
return self._unit
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
|
@ -156,13 +126,13 @@ class CdcSensor(FluNearYouSensor):
|
||||||
@callback
|
@callback
|
||||||
def update_from_latest_data(self):
|
def update_from_latest_data(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
self._attrs.update(
|
self._attr_extra_state_attributes.update(
|
||||||
{
|
{
|
||||||
ATTR_REPORTED_DATE: self.coordinator.data["week_date"],
|
ATTR_REPORTED_DATE: self.coordinator.data["week_date"],
|
||||||
ATTR_STATE: self.coordinator.data["name"],
|
ATTR_STATE: self.coordinator.data["name"],
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self._state = self.coordinator.data[self._sensor_type]
|
self._attr_state = self.coordinator.data[self._sensor_type]
|
||||||
|
|
||||||
|
|
||||||
class UserSensor(FluNearYouSensor):
|
class UserSensor(FluNearYouSensor):
|
||||||
|
@ -171,7 +141,7 @@ class UserSensor(FluNearYouSensor):
|
||||||
@callback
|
@callback
|
||||||
def update_from_latest_data(self):
|
def update_from_latest_data(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
self._attrs.update(
|
self._attr_extra_state_attributes.update(
|
||||||
{
|
{
|
||||||
ATTR_CITY: self.coordinator.data["local"]["city"].split("(")[0],
|
ATTR_CITY: self.coordinator.data["local"]["city"].split("(")[0],
|
||||||
ATTR_REPORTED_LATITUDE: self.coordinator.data["local"]["latitude"],
|
ATTR_REPORTED_LATITUDE: self.coordinator.data["local"]["latitude"],
|
||||||
|
@ -186,15 +156,15 @@ class UserSensor(FluNearYouSensor):
|
||||||
elif self._sensor_type in EXTENDED_SENSOR_TYPE_MAPPING:
|
elif self._sensor_type in EXTENDED_SENSOR_TYPE_MAPPING:
|
||||||
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self._sensor_type]
|
states_key = EXTENDED_SENSOR_TYPE_MAPPING[self._sensor_type]
|
||||||
|
|
||||||
self._attrs[ATTR_STATE_REPORTS_THIS_WEEK] = self.coordinator.data["state"][
|
self._attr_extra_state_attributes[
|
||||||
"data"
|
ATTR_STATE_REPORTS_THIS_WEEK
|
||||||
][states_key]
|
] = self.coordinator.data["state"]["data"][states_key]
|
||||||
self._attrs[ATTR_STATE_REPORTS_LAST_WEEK] = self.coordinator.data["state"][
|
self._attr_extra_state_attributes[
|
||||||
"last_week_data"
|
ATTR_STATE_REPORTS_LAST_WEEK
|
||||||
][states_key]
|
] = self.coordinator.data["state"]["last_week_data"][states_key]
|
||||||
|
|
||||||
if self._sensor_type == SENSOR_TYPE_USER_TOTAL:
|
if self._sensor_type == SENSOR_TYPE_USER_TOTAL:
|
||||||
self._state = sum(
|
self._attr_state = sum(
|
||||||
v
|
v
|
||||||
for k, v in self.coordinator.data["local"].items()
|
for k, v in self.coordinator.data["local"].items()
|
||||||
if k
|
if k
|
||||||
|
@ -207,4 +177,4 @@ class UserSensor(FluNearYouSensor):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self._state = self.coordinator.data["local"][self._sensor_type]
|
self._attr_state = self.coordinator.data["local"][self._sensor_type]
|
||||||
|
|
Loading…
Add table
Reference in a new issue