mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Init entities as unavailable when offline (#29738)
This commit is contained in:
parent
52818bdb89
commit
80be3b74a9
@ -10,7 +10,6 @@ import async_timeout
|
|||||||
|
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.core import Config, HomeAssistant
|
from homeassistant.core import Config, HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.util import Throttle
|
from homeassistant.util import Throttle
|
||||||
|
|
||||||
@ -48,9 +47,6 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
|
|
||||||
await airly.async_update()
|
await airly.async_update()
|
||||||
|
|
||||||
if not airly.data:
|
|
||||||
raise ConfigEntryNotReady()
|
|
||||||
|
|
||||||
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = airly
|
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = airly
|
||||||
|
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
|
@ -5,7 +5,7 @@ from homeassistant.components.air_quality import (
|
|||||||
ATTR_PM_10,
|
ATTR_PM_10,
|
||||||
AirQualityEntity,
|
AirQualityEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
ATTR_API_ADVICE,
|
ATTR_API_ADVICE,
|
||||||
@ -35,10 +35,13 @@ LABEL_PM_10_PERCENT = f"{ATTR_PM_10}_percent_of_limit"
|
|||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up Airly air_quality entity based on a config entry."""
|
"""Set up Airly air_quality entity based on a config entry."""
|
||||||
name = config_entry.data[CONF_NAME]
|
name = config_entry.data[CONF_NAME]
|
||||||
|
latitude = config_entry.data[CONF_LATITUDE]
|
||||||
|
longitude = config_entry.data[CONF_LONGITUDE]
|
||||||
|
unique_id = f"{latitude}-{longitude}"
|
||||||
|
|
||||||
data = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
data = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
|
|
||||||
async_add_entities([AirlyAirQuality(data, name)], True)
|
async_add_entities([AirlyAirQuality(data, name, unique_id)], True)
|
||||||
|
|
||||||
|
|
||||||
def round_state(func):
|
def round_state(func):
|
||||||
@ -56,11 +59,12 @@ def round_state(func):
|
|||||||
class AirlyAirQuality(AirQualityEntity):
|
class AirlyAirQuality(AirQualityEntity):
|
||||||
"""Define an Airly air quality."""
|
"""Define an Airly air quality."""
|
||||||
|
|
||||||
def __init__(self, airly, name):
|
def __init__(self, airly, name, unique_id):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self.airly = airly
|
self.airly = airly
|
||||||
self.data = airly.data
|
self.data = airly.data
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self._unique_id = unique_id
|
||||||
self._pm_2_5 = None
|
self._pm_2_5 = None
|
||||||
self._pm_10 = None
|
self._pm_10 = None
|
||||||
self._aqi = None
|
self._aqi = None
|
||||||
@ -108,12 +112,12 @@ class AirlyAirQuality(AirQualityEntity):
|
|||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
return f"{self.airly.latitude}-{self.airly.longitude}"
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return bool(self.airly.data)
|
return bool(self.data)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
@ -132,7 +136,6 @@ class AirlyAirQuality(AirQualityEntity):
|
|||||||
|
|
||||||
if self.airly.data:
|
if self.airly.data:
|
||||||
self.data = self.airly.data
|
self.data = self.airly.data
|
||||||
|
self._pm_10 = self.data[ATTR_API_PM10]
|
||||||
self._pm_10 = self.data[ATTR_API_PM10]
|
self._pm_2_5 = self.data[ATTR_API_PM25]
|
||||||
self._pm_2_5 = self.data[ATTR_API_PM25]
|
self._aqi = self.data[ATTR_API_CAQI]
|
||||||
self._aqi = self.data[ATTR_API_CAQI]
|
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_ATTRIBUTION,
|
ATTR_ATTRIBUTION,
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
|
CONF_LATITUDE,
|
||||||
|
CONF_LONGITUDE,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_PRESSURE,
|
DEVICE_CLASS_PRESSURE,
|
||||||
@ -60,12 +62,16 @@ SENSOR_TYPES = {
|
|||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Set up Airly sensor entities based on a config entry."""
|
"""Set up Airly sensor entities based on a config entry."""
|
||||||
name = config_entry.data[CONF_NAME]
|
name = config_entry.data[CONF_NAME]
|
||||||
|
latitude = config_entry.data[CONF_LATITUDE]
|
||||||
|
longitude = config_entry.data[CONF_LONGITUDE]
|
||||||
|
|
||||||
data = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
data = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
for sensor in SENSOR_TYPES:
|
for sensor in SENSOR_TYPES:
|
||||||
sensors.append(AirlySensor(data, name, sensor))
|
unique_id = f"{latitude}-{longitude}-{sensor.lower()}"
|
||||||
|
sensors.append(AirlySensor(data, name, sensor, unique_id))
|
||||||
|
|
||||||
async_add_entities(sensors, True)
|
async_add_entities(sensors, True)
|
||||||
|
|
||||||
|
|
||||||
@ -84,11 +90,12 @@ def round_state(func):
|
|||||||
class AirlySensor(Entity):
|
class AirlySensor(Entity):
|
||||||
"""Define an Airly sensor."""
|
"""Define an Airly sensor."""
|
||||||
|
|
||||||
def __init__(self, airly, name, kind):
|
def __init__(self, airly, name, kind, unique_id):
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self.airly = airly
|
self.airly = airly
|
||||||
self.data = airly.data
|
self.data = airly.data
|
||||||
self._name = name
|
self._name = name
|
||||||
|
self._unique_id = unique_id
|
||||||
self.kind = kind
|
self.kind = kind
|
||||||
self._device_class = None
|
self._device_class = None
|
||||||
self._state = None
|
self._state = None
|
||||||
@ -130,7 +137,7 @@ class AirlySensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return a unique_id for this entity."""
|
"""Return a unique_id for this entity."""
|
||||||
return f"{self.airly.latitude}-{self.airly.longitude}-{self.kind.lower()}"
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
@ -140,7 +147,7 @@ class AirlySensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return bool(self.airly.data)
|
return bool(self.data)
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user