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

View file

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