Skip to content

Commit

Permalink
apply converters
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Sep 20, 2024
1 parent b47daa4 commit 11a04e5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 9 deletions.
1 change: 0 additions & 1 deletion custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,6 @@ async def async_update(self):
async def async_update_for_main_entity(self):
if self._miot_service:
for d in [
'sensor', 'binary_sensor', 'switch', 'number', 'select',
'fan', 'cover', 'button', 'scanner', 'number_select',
]:
pls = self.custom_config_list(f'{d}_properties') or []
Expand Down
3 changes: 2 additions & 1 deletion custom_components/xiaomi_miot/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
BinarySensorEntity as BaseEntity,
BinarySensorDeviceClass,
)
from homeassistant.helpers.restore_state import RestoreEntity

from . import (
DOMAIN,
Expand Down Expand Up @@ -85,7 +86,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
bind_services_to_entries(hass, SERVICE_TO_METHOD)


class BinarySensorEntity(XEntity, BaseEntity):
class BinarySensorEntity(XEntity, BaseEntity, RestoreEntity):
def set_state(self, data: dict):
val = data.get(self.attr)
if val is None:
Expand Down
17 changes: 17 additions & 0 deletions custom_components/xiaomi_miot/core/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class BaseConv:
mi: str | int = None
option: dict = None

def __post_init__(self):
self.option = {}

# to hass
def decode(self, device: 'Device', payload: dict, value):
payload[self.attr] = value
Expand All @@ -32,7 +35,21 @@ def encode(self, device: 'Device', payload: dict, value):
@dataclass
class MiotPropConv(BaseConv):
prop: 'MiotProperty' = None
desc: bool = False

def __post_init__(self):
super().__post_init__()
if not self.mi:
self.mi = MiotSpec.unique_prop(self.prop.siid, piid=self.prop.iid)

def decode(self, device: 'Device', payload: dict, value):
if self.desc:
payload['property_value'] = value
value = self.prop.list_description(value)
super().decode(device, payload, value)

# from hass
def encode(self, device: 'Device', payload: dict, value):
if self.desc:
value = self.prop.list_value(value)
super().encode(device, payload, value)
3 changes: 2 additions & 1 deletion custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ def init_converters(self):
if not pls:
continue
for prop in self.spec.get_properties(*pls):
self.converters.append(MiotPropConv(prop.full_name, d, prop=prop))
desc = prop.value_list and d in ['sensor', 'select']
self.converters.append(MiotPropConv(prop.full_name, d, prop=prop, desc=desc))

def add_entity(self, entity: 'XEntity'):
if entity not in self.entities:
Expand Down
6 changes: 5 additions & 1 deletion custom_components/xiaomi_miot/core/hass_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ def on_init(self):
def on_device_update(self, data: dict):
state_change = False

if self.listen_attrs & data.keys():
if keys := self.listen_attrs & data.keys():
self.set_state(data)
state_change = True
self._attr_available = True
for key in keys:
if key == self.attr:
continue
self._attr_extra_state_attributes[key] = data.get(key)

if state_change and self.added:
self._async_write_ha_state()
Expand Down
29 changes: 25 additions & 4 deletions custom_components/xiaomi_miot/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

from homeassistant.components.select import (
DOMAIN as ENTITY_DOMAIN,
SelectEntity,
SelectEntity as BaseEntity,
)
from homeassistant.helpers.restore_state import RestoreEntity

from . import (
DOMAIN,
CONF_MODEL,
XIAOMI_CONFIG_SCHEMA as PLATFORM_SCHEMA, # noqa: F401
HassEntry,
XEntity,
MiotEntity,
BaseSubEntity,
MiotPropertySubEntity,
Expand Down Expand Up @@ -53,7 +55,26 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
bind_services_to_entries(hass, SERVICE_TO_METHOD)


class MiotSelectEntity(MiotEntity, SelectEntity):
class SelectEntity(XEntity, BaseEntity, RestoreEntity):
def on_init(self):
if self._miot_property:
self.listen_attrs.add('property_value')
self._attr_options = self._miot_property.list_descriptions()

def get_state(self) -> dict:
return {self.attr: self._attr_current_option}

def set_state(self, data: dict):
val = data.get(self.attr)
self._attr_current_option = val

async def async_select_option(self, option: str):
await self.device.async_write({self.attr: option})

XEntity.CLS[ENTITY_DOMAIN] = SelectEntity


class MiotSelectEntity(MiotEntity, BaseEntity):
def __init__(self, config, miot_service: MiotService):
super().__init__(miot_service, config=config, logger=_LOGGER)
self._attr_current_option = None
Expand Down Expand Up @@ -93,7 +114,7 @@ def select_option(self, option):
return ret


class MiotSelectSubEntity(SelectEntity, MiotPropertySubEntity):
class MiotSelectSubEntity(BaseEntity, MiotPropertySubEntity):
def __init__(self, parent, miot_property: MiotProperty, option=None):
MiotPropertySubEntity.__init__(self, parent, miot_property, option, domain=ENTITY_DOMAIN)
self._attr_options = miot_property.list_descriptions()
Expand Down Expand Up @@ -161,7 +182,7 @@ def select_option(self, option):
return ret


class SelectSubEntity(SelectEntity, BaseSubEntity):
class SelectSubEntity(BaseEntity, BaseSubEntity):
def __init__(self, parent, attr, option=None):
BaseSubEntity.__init__(self, parent, attr, option)
self._available = True
Expand Down
3 changes: 2 additions & 1 deletion custom_components/xiaomi_miot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ def datetime_with_tzinfo(value):
return value


class SensorEntity(XEntity, BaseEntity):
class SensorEntity(XEntity, BaseEntity, RestoreEntity):
def on_init(self):
if self._miot_property:
self.listen_attrs.add('property_value')
self._attr_icon = self._miot_property.entity_icon
self._attr_device_class = self._miot_property.device_class
self._attr_native_unit_of_measurement = self._miot_property.unit_of_measurement
Expand Down

0 comments on commit 11a04e5

Please sign in to comment.