Skip to content

Commit

Permalink
device available
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Oct 28, 2024
1 parent 84ad2fe commit 2e76c97
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions custom_components/xiaomi_miot/core/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def decode(self, device: 'Device', payload: dict, value):
'mac': device.info.mac,
'lan_ip': device.info.host,
'app_link': device.app_link,
'available': device.available,
'icon': self.option.get('icon') if device.available else 'mdi:information-off',
'updater': updater or 'none',
'updated_at': str(device.data.get('updated', '')),
}
Expand Down
10 changes: 9 additions & 1 deletion custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Device(CustomConfigHelper):
cloud: Optional['MiotCloud'] = None
local: Optional['MiotDevice'] = None
miio2miot: Optional['Miio2MiotHelper'] = None
available = True
miot_entity = None
miot_results = None
_local_state = None
Expand Down Expand Up @@ -649,6 +650,7 @@ async def update_miot_status(
mapping=mapp,
)
results.extend(res)
self.available = True
self._local_state = True
self.miot_results.updater = 'local'
self.miot_results.set_results(results, mapping)
Expand All @@ -659,6 +661,7 @@ async def update_miot_status(
log = _LOGGER.warning if self._local_state else _LOGGER.info
else:
self.miot_results.errors = exc
self.available = False
self._local_state = False
log(
'%s: Got MiioException while fetching the state: %s, mapping: %s, max_properties: %s/%s',
Expand All @@ -667,12 +670,14 @@ async def update_miot_status(

if use_cloud:
try:
self.miot_results.updater = 'cloud'
results = await self.cloud.async_get_properties_for_mapping(self.did, mapping)
if check_lan and self.local:
await self.hass.async_add_executor_job(partial(self.local.info, skip_cache=True))
self.miot_results.updater = 'cloud'
self.available = True
self.miot_results.set_results(results, mapping)
except MiCloudException as exc:
self.available = False
self.miot_results.errors = exc
_LOGGER.error(
'%s: Got MiCloudException while fetching the state: %s, mapping: %s',
Expand Down Expand Up @@ -879,6 +884,7 @@ async def update_cloud_statistics(self, commands=None):
elif isinstance(rls, dict):
update_attrs_with_suffix(attrs, rls)
if attrs:
self.available = True
self.props.update(attrs)
self.data['updated'] = dt.now()
self.dispatch(self.decode_attrs(attrs))
Expand Down Expand Up @@ -924,6 +930,7 @@ async def update_miio_cloud_records(self, keys=None):
else:
attrs[f'{typ}.{key}'] = rls
if attrs:
self.available = True
self.props.update(attrs)
self.data['updated'] = dt.now()
self.dispatch(self.decode_attrs(attrs))
Expand Down Expand Up @@ -960,6 +967,7 @@ async def update_miio_cloud_props(self, keys=None):
else:
attrs = props
if attrs:
self.available = True
self.props.update(attrs)
self.data['updated'] = dt.now()
self.dispatch(self.decode_attrs(attrs))
Expand Down
4 changes: 3 additions & 1 deletion custom_components/xiaomi_miot/core/hass_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ async def async_update(self):

def on_device_update(self, data: dict, only_info=False):
state_change = False
self._attr_available = True
self._attr_available = self.device.available

if isinstance(self.conv, InfoConv):
self._attr_available = True
self._attr_icon = data.get('icon', self._attr_icon)
self._attr_extra_state_attributes.update(data)
elif only_info:
return
Expand Down
3 changes: 1 addition & 2 deletions custom_components/xiaomi_miot/core/hass_entry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import logging
from typing import TYPE_CHECKING, Optional
from typing import TYPE_CHECKING
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_USERNAME
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .miio2miot import Miio2MiotHelper
from .xiaomi_cloud import MiotCloud

if TYPE_CHECKING:
Expand Down

0 comments on commit 2e76c97

Please sign in to comment.