Small cleanups to esphome sensor and binary_sensor (#95042)

This commit is contained in:
J. Nick Koston 2023-06-22 11:14:33 +02:00 committed by GitHub
parent 5884afd485
commit cd5fdb97c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 13 deletions

View file

@ -51,9 +51,8 @@ class EsphomeBinarySensor(
return self._entry_data.available
if not self._has_state:
return None
if self._state.missing_state:
return None
return self._state.state
state = self._state
return None if state.missing_state else state.state
@callback
def _on_static_info_update(self, static_info: EntityInfo) -> None:
@ -66,9 +65,7 @@ class EsphomeBinarySensor(
@property
def available(self) -> bool:
"""Return True if entity is available."""
if self._static_info.is_status_binary_sensor:
return True
return super().available
return self._static_info.is_status_binary_sensor or super().available
class EsphomeAssistInProgressBinarySensor(EsphomeAssistEntity, BinarySensorEntity):

View file

@ -94,13 +94,14 @@ class EsphomeSensor(EsphomeEntity[SensorInfo, SensorState], SensorEntity):
@esphome_state_property
def native_value(self) -> datetime | str | None:
"""Return the state of the entity."""
if math.isnan(self._state.state):
state = self._state
if math.isnan(state.state):
return None
if self._state.missing_state:
if state.missing_state:
return None
if self._attr_device_class == SensorDeviceClass.TIMESTAMP:
return dt_util.utc_from_timestamp(self._state.state)
return f"{self._state.state:.{self._static_info.accuracy_decimals}f}"
return dt_util.utc_from_timestamp(state.state)
return f"{state.state:.{self._static_info.accuracy_decimals}f}"
class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEntity):
@ -110,6 +111,5 @@ class EsphomeTextSensor(EsphomeEntity[TextSensorInfo, TextSensorState], SensorEn
@esphome_state_property
def native_value(self) -> str | None:
"""Return the state of the entity."""
if self._state.missing_state:
return None
return self._state.state
state = self._state
return None if state.missing_state else state.state