From 65852683f40a43959cc3db459d2d7e997cc56e69 Mon Sep 17 00:00:00 2001 From: Alone Date: Tue, 24 Sep 2024 11:44:09 +0800 Subject: [PATCH] apply converter for text --- custom_components/xiaomi_miot/__init__.py | 2 +- custom_components/xiaomi_miot/core/device.py | 2 +- .../xiaomi_miot/core/hass_entity.py | 5 +- .../xiaomi_miot/core/hass_entry.py | 1 + custom_components/xiaomi_miot/text.py | 64 +++++++++---------- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/custom_components/xiaomi_miot/__init__.py b/custom_components/xiaomi_miot/__init__.py index 0811befd26..feff50d6f2 100644 --- a/custom_components/xiaomi_miot/__init__.py +++ b/custom_components/xiaomi_miot/__init__.py @@ -1260,7 +1260,7 @@ async def async_update_for_main_entity(self): pls = self.custom_config_list(f'{d}_properties') or [] if pls: self._update_sub_entities(pls, '*', domain=d) - for d in ['text', 'select']: + for d in ['select']: als = self.custom_config_list(f'{d}_actions') or [] if als: self._update_sub_entities(None, '*', domain=d, actions=als) diff --git a/custom_components/xiaomi_miot/core/device.py b/custom_components/xiaomi_miot/core/device.py index e0942fce57..58d72e8d9d 100644 --- a/custom_components/xiaomi_miot/core/device.py +++ b/custom_components/xiaomi_miot/core/device.py @@ -258,7 +258,7 @@ def init_converters(self): desc = bool(prop.value_list and d in ['sensor', 'select']) self.converters.append(MiotPropConv(prop.full_name, d, prop=prop, desc=desc)) - for d in ['button']: + for d in ['button', 'text']: als = self.custom_config_list(f'{d}_actions') or [] if not als: continue diff --git a/custom_components/xiaomi_miot/core/hass_entity.py b/custom_components/xiaomi_miot/core/hass_entity.py index 480c1e044f..e747cf4a8c 100644 --- a/custom_components/xiaomi_miot/core/hass_entity.py +++ b/custom_components/xiaomi_miot/core/hass_entity.py @@ -102,9 +102,10 @@ def __init__(self, device: 'Device', conv: 'BaseConv'): self._miot_service = conv.action.service self._miot_action = conv.action self._miot_property = conv.action.in_properties()[0] if conv.action.ins else None + self._attr_available = True else: - prefix = device.spec.generate_entity_id(self) + prefix = device.spec.generate_entity_id(self, self.attr) self.entity_id = f'{prefix}_{self.attr}' self._attr_name = self.attr.replace('_', '').title() self._attr_translation_key = self.attr @@ -127,11 +128,11 @@ def on_init(self): def on_device_update(self, data: dict): state_change = False + self._attr_available = True 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 diff --git a/custom_components/xiaomi_miot/core/hass_entry.py b/custom_components/xiaomi_miot/core/hass_entry.py index b72d783609..def9c8e5b0 100644 --- a/custom_components/xiaomi_miot/core/hass_entry.py +++ b/custom_components/xiaomi_miot/core/hass_entry.py @@ -51,6 +51,7 @@ async def new_device(self, device_info: dict, cloud: Optional[MiotCloud] = None) spec = await device.get_spec() if spec and not device.cloud_only: device.miio2miot = Miio2MiotHelper.from_model(self.hass, device.model, spec) + await device.update_miot_status() self.devices[info.unique_id] = device return device diff --git a/custom_components/xiaomi_miot/text.py b/custom_components/xiaomi_miot/text.py index 421347c918..f6ba5817e7 100644 --- a/custom_components/xiaomi_miot/text.py +++ b/custom_components/xiaomi_miot/text.py @@ -4,25 +4,19 @@ from homeassistant.components.text import ( DOMAIN as ENTITY_DOMAIN, - TextEntity, + TextEntity as BaseEntity, ) from . import ( DOMAIN, - CONF_MODEL, XIAOMI_CONFIG_SCHEMA as PLATFORM_SCHEMA, # noqa: F401 HassEntry, - MiotEntity, + XEntity, MiotPropertySubEntity, BaseSubEntity, async_setup_config_entry, - bind_services_to_entries, -) -from .core.miot_spec import ( - MiotSpec, - MiotService, - MiotAction, ) +from .core.miot_spec import MiotAction _LOGGER = logging.getLogger(__name__) DATA_KEY = f'{ENTITY_DOMAIN}.{DOMAIN}' @@ -37,35 +31,37 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): hass.data.setdefault(DATA_KEY, {}) - hass.data[DOMAIN]['add_entities'][ENTITY_DOMAIN] = async_add_entities - config['hass'] = hass - model = str(config.get(CONF_MODEL) or '') - spec = hass.data[DOMAIN]['miot_specs'].get(model) - entities = [] - if isinstance(spec, MiotSpec): - for srv in spec.get_services('none_service'): - if not srv.get_property('none_property'): - continue - entities.append(MiotTextEntity(config, srv)) - for entity in entities: - hass.data[DOMAIN]['entities'][entity.unique_id] = entity - async_add_entities(entities, update_before_add=True) - bind_services_to_entries(hass, SERVICE_TO_METHOD) - - -class MiotTextEntity(MiotEntity, TextEntity): + + +class TextEntity(XEntity, BaseEntity): _attr_native_value = '' - def __init__(self, config, miot_service: MiotService): - super().__init__(miot_service, config=config, logger=_LOGGER) + def get_state(self) -> dict: + return {self.attr: self._attr_native_value} - def set_value(self, value): - """Change the value.""" - self._attr_native_value = value - raise NotImplementedError() + def set_state(self, data: dict): + val = data.get(self.attr) + if isinstance(val, list): + val = val[0] if val else None + if val is None: + val = '' + self._attr_native_value = val + + async def async_set_value(self, value: str): + if self._miot_action and self._miot_action.name == 'execute_text_directive': + silent = self.custom_config_integer('silent_execution', 0) + value = [value, silent] + + await self.device.async_write({self.attr: value}) + + if self._miot_action: + self._attr_native_value = '' + self.schedule_update_ha_state() + +XEntity.CLS[ENTITY_DOMAIN] = TextEntity -class MiotTextSubEntity(MiotPropertySubEntity, TextEntity): +class MiotTextSubEntity(MiotPropertySubEntity, BaseEntity): _attr_native_value = '' def update(self, data=None): @@ -80,7 +76,7 @@ def set_value(self, value): return self.set_parent_property(value) -class MiotTextActionSubEntity(BaseSubEntity, TextEntity): +class MiotTextActionSubEntity(BaseSubEntity, BaseEntity): _attr_native_value = '' def __init__(self, parent, miot_action: MiotAction, option=None):