Make Xiaomi Miio unavailable device independent (#47795)

* make unavailable independent

* fix data is None

* process review comments
pull/47946/head
starkillerOG 2021-03-15 12:25:11 +01:00 committed by GitHub
parent b2efcb3c22
commit 1aa4fd4cc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 8 deletions

View File

@ -7,9 +7,10 @@ from miio.gateway.gateway import GatewayException
from homeassistant import config_entries, core
from homeassistant.const import CONF_HOST, CONF_TOKEN
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 (
ATTR_AVAILABLE,
CONF_DEVICE,
CONF_FLOW_TYPE,
CONF_GATEWAY,
@ -86,13 +87,22 @@ async def async_setup_gateway_entry(
sw_version=gateway_info.firmware_version,
)
async def async_update_data():
def update_data():
"""Fetch data from the subdevice."""
try:
data = {}
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:
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
coordinator = DataUpdateCoordinator(

View File

@ -9,6 +9,8 @@ CONF_MAC = "mac"
KEY_COORDINATOR = "coordinator"
ATTR_AVAILABLE = "available"
# Fan Models
MODEL_AIRPURIFIER_V1 = "zhimi.airpurifier.v1"
MODEL_AIRPURIFIER_V2 = "zhimi.airpurifier.v2"

View File

@ -6,7 +6,7 @@ from miio import DeviceException, gateway
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .const import ATTR_AVAILABLE, DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -89,3 +89,11 @@ class XiaomiGatewayDevice(CoordinatorEntity, Entity):
"model": self._sub_device.model,
"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]