Skip to content

Commit

Permalink
fix: fix temperature native_unit_of_measurement (#2521)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
danielbrunt57 and pre-commit-ci[bot] authored Sep 14, 2024
1 parent 217589e commit e55b198
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
5 changes: 3 additions & 2 deletions custom_components/alexa_media/alexa_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
51 changes: 44 additions & 7 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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},
Expand All @@ -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."""
Expand Down

0 comments on commit e55b198

Please sign in to comment.