mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Update coronavirus to use CoordinatorEntity (#39449)
* Update coronavirus to use CoordinatorEntity * Remove should_poll from coronavirus
This commit is contained in:
parent
692ed8c639
commit
e5b360cd08
@ -1,6 +1,6 @@
|
|||||||
"""Sensor platform for the Corona virus."""
|
"""Sensor platform for the Corona virus."""
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION
|
from homeassistant.const import ATTR_ATTRIBUTION
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import get_coordinator
|
from . import get_coordinator
|
||||||
from .const import ATTRIBUTION, OPTION_WORLDWIDE
|
from .const import ATTRIBUTION, OPTION_WORLDWIDE
|
||||||
@ -23,7 +23,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class CoronavirusSensor(Entity):
|
class CoronavirusSensor(CoordinatorEntity):
|
||||||
"""Sensor representing corona virus data."""
|
"""Sensor representing corona virus data."""
|
||||||
|
|
||||||
name = None
|
name = None
|
||||||
@ -31,12 +31,12 @@ class CoronavirusSensor(Entity):
|
|||||||
|
|
||||||
def __init__(self, coordinator, country, info_type):
|
def __init__(self, coordinator, country, info_type):
|
||||||
"""Initialize coronavirus sensor."""
|
"""Initialize coronavirus sensor."""
|
||||||
|
super().__init__(coordinator)
|
||||||
if country == OPTION_WORLDWIDE:
|
if country == OPTION_WORLDWIDE:
|
||||||
self.name = f"Worldwide Coronavirus {info_type}"
|
self.name = f"Worldwide Coronavirus {info_type}"
|
||||||
else:
|
else:
|
||||||
self.name = f"{coordinator.data[country].country} Coronavirus {info_type}"
|
self.name = f"{coordinator.data[country].country} Coronavirus {info_type}"
|
||||||
self.unique_id = f"{country}-{info_type}"
|
self.unique_id = f"{country}-{info_type}"
|
||||||
self.coordinator = coordinator
|
|
||||||
self.country = country
|
self.country = country
|
||||||
self.info_type = info_type
|
self.info_type = info_type
|
||||||
|
|
||||||
@ -76,11 +76,3 @@ class CoronavirusSensor(Entity):
|
|||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return device attributes."""
|
"""Return device attributes."""
|
||||||
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""When entity is added to hass."""
|
|
||||||
self.coordinator.async_add_listener(self.async_write_ha_state)
|
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
|
||||||
"""When entity will be removed from hass."""
|
|
||||||
self.coordinator.async_remove_listener(self.async_write_ha_state)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user