mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
Make Xiaomi Miio unavailable device independent (#47795)
* make unavailable independent * fix data is None * process review comments
This commit is contained in:
parent
b2efcb3c22
commit
1aa4fd4cc9
@ -7,9 +7,10 @@ from miio.gateway.gateway import GatewayException
|
|||||||
from homeassistant import config_entries, core
|
from homeassistant import config_entries, core
|
||||||
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
from homeassistant.const import CONF_HOST, CONF_TOKEN
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
ATTR_AVAILABLE,
|
||||||
CONF_DEVICE,
|
CONF_DEVICE,
|
||||||
CONF_FLOW_TYPE,
|
CONF_FLOW_TYPE,
|
||||||
CONF_GATEWAY,
|
CONF_GATEWAY,
|
||||||
@ -86,13 +87,22 @@ async def async_setup_gateway_entry(
|
|||||||
sw_version=gateway_info.firmware_version,
|
sw_version=gateway_info.firmware_version,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_update_data():
|
def update_data():
|
||||||
"""Fetch data from the subdevice."""
|
"""Fetch data from the subdevice."""
|
||||||
try:
|
data = {}
|
||||||
for sub_device in gateway.gateway_device.devices.values():
|
for sub_device in gateway.gateway_device.devices.values():
|
||||||
await hass.async_add_executor_job(sub_device.update)
|
try:
|
||||||
|
sub_device.update()
|
||||||
except GatewayException as ex:
|
except GatewayException as ex:
|
||||||
raise UpdateFailed("Got exception while fetching the state") from ex
|
_LOGGER.error("Got exception while fetching the state: %s", ex)
|
||||||
|
data[sub_device.sid] = {ATTR_AVAILABLE: False}
|
||||||
|
else:
|
||||||
|
data[sub_device.sid] = {ATTR_AVAILABLE: True}
|
||||||
|
return data
|
||||||
|
|
||||||
|
async def async_update_data():
|
||||||
|
"""Fetch data from the subdevice using async_add_executor_job."""
|
||||||
|
return await hass.async_add_executor_job(update_data)
|
||||||
|
|
||||||
# Create update coordinator
|
# Create update coordinator
|
||||||
coordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator(
|
||||||
|
@ -9,6 +9,8 @@ CONF_MAC = "mac"
|
|||||||
|
|
||||||
KEY_COORDINATOR = "coordinator"
|
KEY_COORDINATOR = "coordinator"
|
||||||
|
|
||||||
|
ATTR_AVAILABLE = "available"
|
||||||
|
|
||||||
# Fan Models
|
# Fan Models
|
||||||
MODEL_AIRPURIFIER_V1 = "zhimi.airpurifier.v1"
|
MODEL_AIRPURIFIER_V1 = "zhimi.airpurifier.v1"
|
||||||
MODEL_AIRPURIFIER_V2 = "zhimi.airpurifier.v2"
|
MODEL_AIRPURIFIER_V2 = "zhimi.airpurifier.v2"
|
||||||
|
@ -6,7 +6,7 @@ from miio import DeviceException, gateway
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import ATTR_AVAILABLE, DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -89,3 +89,11 @@ class XiaomiGatewayDevice(CoordinatorEntity, Entity):
|
|||||||
"model": self._sub_device.model,
|
"model": self._sub_device.model,
|
||||||
"sw_version": self._sub_device.firmware_version,
|
"sw_version": self._sub_device.firmware_version,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@property
|
||||||
|
def available(self):
|
||||||
|
"""Return if entity is available."""
|
||||||
|
if self.coordinator.data is None:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return self.coordinator.data[self._sub_device.sid][ATTR_AVAILABLE]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user