feat: support binary mode

This commit is contained in:
topsworld 2025-01-16 22:33:37 +08:00
parent 37a446f2d1
commit 14766466ca
4 changed files with 38 additions and 4 deletions

View File

@ -217,8 +217,21 @@ async def async_setup_entry(
siid=action.service.iid, aiid=action.iid) siid=action.service.iid, aiid=action.iid)
if er.async_get(entity_id_or_uuid=entity_id): if er.async_get(entity_id_or_uuid=entity_id):
er.async_remove(entity_id=entity_id) er.async_remove(entity_id=entity_id)
# Binary sensor display # Binary sensor display
if not miot_client.display_binary_bool:
for prop in device.prop_list.get('binary_sensor', []):
entity_id = device.gen_prop_entity_id(
ha_domain='binary_sensor', spec_name=prop.name,
siid=prop.service.iid, piid=prop.iid)
if er.async_get(entity_id_or_uuid=entity_id):
er.async_remove(entity_id=entity_id)
if not miot_client.display_binary_text:
for prop in device.prop_list.get('binary_sensor', []):
entity_id = device.gen_prop_entity_id(
ha_domain='sensor', spec_name=prop.name,
siid=prop.service.iid, piid=prop.iid)
if er.async_get(entity_id_or_uuid=entity_id):
er.async_remove(entity_id=entity_id)
hass.data[DOMAIN]['devices'][config_entry.entry_id] = miot_devices hass.data[DOMAIN]['devices'][config_entry.entry_id] = miot_devices
await hass.config_entries.async_forward_entry_setups( await hass.config_entries.async_forward_entry_setups(

View File

@ -68,9 +68,10 @@ async def async_setup_entry(
new_entities = [] new_entities = []
for miot_device in device_list: for miot_device in device_list:
for prop in miot_device.prop_list.get('binary_sensor', []): if miot_device.miot_client.display_binary_bool:
new_entities.append(BinarySensor( for prop in miot_device.prop_list.get('binary_sensor', []):
miot_device=miot_device, spec=prop)) new_entities.append(BinarySensor(
miot_device=miot_device, spec=prop))
if new_entities: if new_entities:
async_add_entities(new_entities) async_add_entities(new_entities)

View File

@ -169,6 +169,8 @@ class MIoTClient:
_show_devices_changed_notify_timer: Optional[asyncio.TimerHandle] _show_devices_changed_notify_timer: Optional[asyncio.TimerHandle]
# Display devices changed notify # Display devices changed notify
_display_devs_notify: list[str] _display_devs_notify: list[str]
_display_binary_text: bool
_display_binary_bool: bool
def __init__( def __init__(
self, self,
@ -235,6 +237,10 @@ class MIoTClient:
self._display_devs_notify = entry_data.get( self._display_devs_notify = entry_data.get(
'display_devices_changed_notify', ['add', 'del', 'offline']) 'display_devices_changed_notify', ['add', 'del', 'offline'])
self._display_binary_text = 'text' in entry_data.get(
'display_binary_mode', ['text'])
self._display_binary_bool = 'bool' in entry_data.get(
'display_binary_mode', ['text'])
async def init_async(self) -> None: async def init_async(self) -> None:
# Load user config and check # Load user config and check
@ -469,6 +475,14 @@ class MIoTClient:
def display_devices_changed_notify(self) -> list[str]: def display_devices_changed_notify(self) -> list[str]:
return self._display_devs_notify return self._display_devs_notify
@property
def display_binary_text(self) -> bool:
return self._display_binary_text
@property
def display_binary_bool(self) -> bool:
return self._display_binary_bool
@display_devices_changed_notify.setter @display_devices_changed_notify.setter
def display_devices_changed_notify(self, value: list[str]) -> None: def display_devices_changed_notify(self, value: list[str]) -> None:
if set(value) == set(self._display_devs_notify): if set(value) == set(self._display_devs_notify):

View File

@ -76,6 +76,12 @@ async def async_setup_entry(
for prop in miot_device.prop_list.get('sensor', []): for prop in miot_device.prop_list.get('sensor', []):
new_entities.append(Sensor(miot_device=miot_device, spec=prop)) new_entities.append(Sensor(miot_device=miot_device, spec=prop))
if miot_device.miot_client.display_binary_text:
for prop in miot_device.prop_list.get('binary_sensor', []):
if not prop.value_list:
continue
new_entities.append(Sensor(miot_device=miot_device, spec=prop))
if new_entities: if new_entities:
async_add_entities(new_entities) async_add_entities(new_entities)