Skip to content

Commit

Permalink
♻️ improve for entity state (#2263)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Jan 2, 2025
1 parent 4bc63a4 commit cfdf1c0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
5 changes: 4 additions & 1 deletion custom_components/xiaomi_miot/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def get_state(self) -> dict:
return {self.attr: self._attr_native_value}

def set_state(self, data: dict):
self._attr_native_value = self.conv.value_from_dict(data)
val = self.conv.value_from_dict(data)
if val is None:
return
self._attr_native_value = val

async def async_set_native_value(self, value: float):
await self.device.async_write({self.attr: value})
Expand Down
4 changes: 3 additions & 1 deletion custom_components/xiaomi_miot/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def get_state(self) -> dict:
return {self.attr: self._attr_current_option}

def set_state(self, data: dict):
val = data.get(self.attr)
val = self.conv.value_from_dict(data)
if val is None:
return
self._attr_current_option = val

async def async_select_option(self, option: str):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/xiaomi_miot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get_state(self) -> dict:
return {self.attr: self._attr_native_value}

def set_state(self, data: dict):
value = data.get(self.attr)
value = self.conv.value_from_dict(data)
prop = self._miot_property
if prop and prop.value_range:
if not prop.range_valid(value):
Expand All @@ -148,7 +148,7 @@ def set_state(self, data: dict):
value = None
if self.device_class == SensorDeviceClass.TIMESTAMP:
value = datetime_with_tzinfo(value)
self._attr_native_value = value
self._attr_native_value = value

@cached_property
def custom_value_ratio(self):
Expand Down
8 changes: 4 additions & 4 deletions custom_components/xiaomi_miot/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ def get_state(self) -> dict:
return {self.attr: self._attr_is_on}

def set_state(self, data: dict):
val = data.get(self.attr)
if val is not None:
val = bool(val)
self._attr_is_on = val
val = self.conv.value_from_dict(data)
if val is None:
return
self._attr_is_on = bool(val)

async def async_turn_on(self):
await self.device.async_write({self.attr: True})
Expand Down
6 changes: 4 additions & 2 deletions custom_components/xiaomi_miot/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ def get_state(self) -> dict:

def set_state(self, data: dict):
val = self.conv.value_from_dict(data)
if val is None:
return
if isinstance(val, list):
val = val[0] if val else None
if val is None:
val = ''
if val is None:
val = ''
self._attr_native_value = val

async def async_set_value(self, value: str):
Expand Down

0 comments on commit cfdf1c0

Please sign in to comment.