Skip to content

Commit

Permalink
🔢 improve for number entity
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed May 20, 2022
1 parent c08d324 commit 9cb43dc
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions custom_components/xiaomi_miot/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
DOMAIN as ENTITY_DOMAIN,
NumberEntity,
)
from homeassistant.helpers.restore_state import RestoreEntity, RestoreStateData

from . import (
DOMAIN,
Expand Down Expand Up @@ -54,32 +55,51 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class MiotNumberEntity(MiotEntity, NumberEntity):
def __init__(self, config, miot_service: MiotService):
super().__init__(miot_service, config=config, logger=_LOGGER)
self._attr_value = 0

@property
def value(self):
"""Return the entity value to represent the entity state."""
return 0

def set_value(self, value: float):
def set_value(self, value):
"""Set new value."""
raise NotImplementedError()


class MiotNumberSubEntity(MiotPropertySubEntity, NumberEntity):
class MiotNumberSubEntity(MiotPropertySubEntity, NumberEntity, RestoreEntity):
def __init__(self, parent, miot_property: MiotProperty, option=None):
super().__init__(parent, miot_property, option, domain=ENTITY_DOMAIN)
self._attr_value = 0
self._is_restore = False

async def async_added_to_hass(self):
await super().async_added_to_hass()
self._is_restore = self.custom_config_bool('restore_state')
if self._is_restore:
restored = await RestoreStateData.async_get_instance(self.hass)
if restored and self.entity_id in restored.last_states:
state = restored.last_states[self.entity_id].state
val = self.cast_value(state.state)
if val is not None:
self._attr_value = val

def update(self, data=None):
super().update(data)
val = self.native_value
if val is not None:
self._attr_value = val

@property
def value(self):
"""Return the entity value to represent the entity state."""
def native_value(self):
val = self._miot_property.from_dict(self._state_attrs)
return self.cast_value(val)

def cast_value(self, val, default=None):
try:
val = float(val)
val = round(float(val), 6)
if self._miot_property.is_integer:
val = int(val)
except (TypeError, ValueError):
val = 0
val = default
return val

def set_value(self, value: float):
def set_value(self, value):
"""Set new value."""
if self._miot_property.is_integer:
value = int(value)
Expand All @@ -105,24 +125,24 @@ class MiotNumberActionSubEntity(MiotNumberSubEntity):
def __init__(self, parent, miot_property: MiotProperty, miot_action: MiotAction, option=None):
super().__init__(parent, miot_property, option)
self._miot_action = miot_action
self._value = 0
self._state_attrs.update({
'miot_action': miot_action.full_name,
})

def update(self, data=None):
self._available = True
self._value = 0
self._attr_value = 0

@property
def value(self):
"""Return the entity value to represent the entity state."""
return self._value
return self._attr_value

def set_value(self, value: float):
"""Set new value."""
val = int(value)
ret = self.call_parent('call_action', self._miot_action, [val])
if ret:
self._value = val
self._attr_value = val
self.async_write_ha_state()
return ret

0 comments on commit 9cb43dc

Please sign in to comment.