Skip to content

Commit

Permalink
apply converter for text
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Sep 24, 2024
1 parent f225f4f commit 6585268
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 38 deletions.
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions custom_components/xiaomi_miot/core/hass_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions custom_components/xiaomi_miot/core/hass_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 30 additions & 34 deletions custom_components/xiaomi_miot/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 6585268

Please sign in to comment.