Small cleanups to shelly (#124758)

This commit is contained in:
J. Nick Koston 2024-08-27 21:00:31 -10:00 committed by GitHub
parent a63c5e6725
commit f9bf7f7e05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 11 deletions

View File

@ -488,7 +488,7 @@ class ShellyRestAttributeEntity(CoordinatorEntity[ShellyBlockCoordinator]):
@property @property
def attribute_value(self) -> StateType: def attribute_value(self) -> StateType:
"""Value of sensor.""" """Value of sensor."""
if callable(self.entity_description.value): if self.entity_description.value is not None:
self._last_value = self.entity_description.value( self._last_value = self.entity_description.value(
self.block_coordinator.device.status, self._last_value self.block_coordinator.device.status, self._last_value
) )
@ -518,7 +518,7 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, Entity):
id_key = key.split(":")[-1] id_key = key.split(":")[-1]
self._id = int(id_key) if id_key.isnumeric() else None self._id = int(id_key) if id_key.isnumeric() else None
if callable(description.unit): if description.unit is not None:
self._attr_native_unit_of_measurement = description.unit( self._attr_native_unit_of_measurement = description.unit(
coordinator.device.config[key] coordinator.device.config[key]
) )
@ -544,7 +544,7 @@ class ShellyRpcAttributeEntity(ShellyRpcEntity, Entity):
@property @property
def attribute_value(self) -> StateType: def attribute_value(self) -> StateType:
"""Value of sensor.""" """Value of sensor."""
if callable(self.entity_description.value): if self.entity_description.value is not None:
# using "get" here since subkey might not exist (e.g. "errors" sub_key) # using "get" here since subkey might not exist (e.g. "errors" sub_key)
self._last_value = self.entity_description.value( self._last_value = self.entity_description.value(
self.status.get(self.entity_description.sub_key), self._last_value self.status.get(self.entity_description.sub_key), self._last_value

View File

@ -207,17 +207,17 @@ class RpcNumber(ShellyRpcAttributeEntity, NumberEntity):
"""Initialize sensor.""" """Initialize sensor."""
super().__init__(coordinator, key, attribute, description) super().__init__(coordinator, key, attribute, description)
if callable(description.max_fn): if description.max_fn is not None:
self._attr_native_max_value = description.max_fn( self._attr_native_max_value = description.max_fn(
coordinator.device.config[key] coordinator.device.config[key]
) )
if callable(description.min_fn): if description.min_fn is not None:
self._attr_native_min_value = description.min_fn( self._attr_native_min_value = description.min_fn(
coordinator.device.config[key] coordinator.device.config[key]
) )
if callable(description.step_fn): if description.step_fn is not None:
self._attr_native_step = description.step_fn(coordinator.device.config[key]) self._attr_native_step = description.step_fn(coordinator.device.config[key])
if callable(description.mode_fn): if description.mode_fn is not None:
self._attr_mode = description.mode_fn(coordinator.device.config[key]) self._attr_mode = description.mode_fn(coordinator.device.config[key])
@property @property

View File

@ -1266,13 +1266,15 @@ class RpcSensor(ShellyRpcAttributeEntity, SensorEntity):
@property @property
def native_value(self) -> StateType: def native_value(self) -> StateType:
"""Return value of sensor.""" """Return value of sensor."""
if not self.option_map: attribute_value = self.attribute_value
return self.attribute_value
if not isinstance(self.attribute_value, str): if not self.option_map:
return attribute_value
if not isinstance(attribute_value, str):
return None return None
return self.option_map[self.attribute_value] return self.option_map[attribute_value]
class BlockSleepingSensor(ShellySleepingBlockAttributeEntity, RestoreSensor): class BlockSleepingSensor(ShellySleepingBlockAttributeEntity, RestoreSensor):