From e55b1987998c3bdd522ef4b3a41d29748c1d7b8e Mon Sep 17 00:00:00 2001 From: Daniel <49846893+danielbrunt57@users.noreply.github.com> Date: Sat, 14 Sep 2024 01:44:43 -0700 Subject: [PATCH] fix: fix temperature native_unit_of_measurement (#2521) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- custom_components/alexa_media/alexa_entity.py | 5 +- custom_components/alexa_media/sensor.py | 51 ++++++++++++++++--- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/custom_components/alexa_media/alexa_entity.py b/custom_components/alexa_media/alexa_entity.py index ccd277b5..2e24f741 100644 --- a/custom_components/alexa_media/alexa_entity.py +++ b/custom_components/alexa_media/alexa_entity.py @@ -350,10 +350,11 @@ def parse_temperature_from_coordinator( coordinator: DataUpdateCoordinator, entity_id: str ) -> Optional[str]: """Get the temperature of an entity from the coordinator data.""" - value = parse_value_from_coordinator( + temperature = parse_value_from_coordinator( coordinator, entity_id, "Alexa.TemperatureSensor", "temperature" ) - return value.get("value") if value and "value" in value else None + _LOGGER.debug("parse_temperature_from_coordinator: %s", temperature) + return temperature def parse_air_quality_from_coordinator( diff --git a/custom_components/alexa_media/sensor.py b/custom_components/alexa_media/sensor.py index 6ad68bba..e66ef2ae 100644 --- a/custom_components/alexa_media/sensor.py +++ b/custom_components/alexa_media/sensor.py @@ -182,9 +182,10 @@ async def create_temperature_sensors(account_dict, temperature_entities): coordinator = account_dict["coordinator"] for temp in temperature_entities: _LOGGER.debug( - "Creating entity %s for a temperature sensor with name %s", + "Creating entity %s for a temperature sensor with name %s (%s)", temp["id"], temp["name"], + temp, ) serial = temp["device_serial"] device_info = lookup_device_info(account_dict, serial) @@ -254,16 +255,25 @@ def __init__(self, coordinator, entity_id, name, media_player_device_id): """Initialize temperature sensor.""" super().__init__(coordinator) self.alexa_entity_id = entity_id + # Need to append "+temperature" because the Alexa entityId is for a physical device + # and a single physical device can have multiple HA entities + self._attr_unique_id = entity_id + "_temperature" self._attr_name = name + " Temperature" self._attr_device_class = SensorDeviceClass.TEMPERATURE self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_value: Optional[datetime.datetime] = ( + value_and_scale: Optional[datetime.datetime] = ( parse_temperature_from_coordinator(coordinator, entity_id) ) - self._attr_native_unit_of_measurement: Optional[str] = UnitOfTemperature.CELSIUS - # This includes "_temperature" because the Alexa entityId is for a physical device - # A single physical device could have multiple HA entities - self._attr_unique_id = entity_id + "_temperature" + self._attr_native_value = self._get_temperature_value(value_and_scale) + self._attr_native_unit_of_measurement = self._get_temperature_scale( + value_and_scale + ) + _LOGGER.debug( + "Coordinator init: %s: %s %s", + self._attr_name, + self._attr_native_value, + self._attr_native_unit_of_measurement, + ) self._attr_device_info = ( { "identifiers": {media_player_device_id}, @@ -276,11 +286,38 @@ def __init__(self, coordinator, entity_id, name, media_player_device_id): @callback def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" - self._attr_native_value = parse_temperature_from_coordinator( + value_and_scale = parse_temperature_from_coordinator( self.coordinator, self.alexa_entity_id ) + self._attr_native_value = self._get_temperature_value(value_and_scale) + self._attr_native_unit_of_measurement = self._get_temperature_scale( + value_and_scale + ) + _LOGGER.debug( + "Coordinator update: %s: %s %s", + self._attr_name, + self._attr_native_value, + self._attr_native_unit_of_measurement, + ) super()._handle_coordinator_update() + def _get_temperature_value(self, value): + if value and "value" in value: + _LOGGER.debug("TemperatureSensor value: %s", value.get("value")) + return value.get("value") + return None + + def _get_temperature_scale(self, value): + if value and "scale" in value: + _LOGGER.debug("TemperatureSensor scale: %s", value.get("scale")) + if value.get("scale") == "CELSIUS": + return UnitOfTemperature.CELSIUS + if value.get("scale") == "FAHRENHEIT": + return UnitOfTemperature.FAHRENHEIT + if value.get("scale") == "KELVIN": + return UnitOfTemperature.KELVIN + return None + class AirQualitySensor(SensorEntity, CoordinatorEntity): """A air quality sensor reported by an Amazon indoor air quality monitor."""