From 7cb44b20b6249336b01628e290ee943053a3fe1d Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Sat, 8 Jun 2024 02:57:58 -0400 Subject: [PATCH] fix: remove pytz to fix blocking (#2281) closes #2277 --- custom_components/alexa_media/sensor.py | 32 +++++++++++++------------ 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/custom_components/alexa_media/sensor.py b/custom_components/alexa_media/sensor.py index a49e7859..ee585f4a 100644 --- a/custom_components/alexa_media/sensor.py +++ b/custom_components/alexa_media/sensor.py @@ -6,6 +6,7 @@ For more details about this platform, please refer to the documentation at https://community.home-assistant.io/t/echo-devices-alexa-as-media-player-testers-needed/58639 """ + import datetime import json import logging @@ -24,7 +25,6 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt from packaging import version -import pytz from . import ( CONF_EMAIL, @@ -257,9 +257,9 @@ def __init__(self, coordinator, entity_id, name, media_player_device_id): self._attr_name = name + " Temperature" self._attr_device_class = SensorDeviceClass.TEMPERATURE self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_value: Optional[ - datetime.datetime - ] = parse_temperature_from_coordinator(coordinator, entity_id) + self._attr_native_value: 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 @@ -306,12 +306,12 @@ def __init__( self._attr_name = name + " " + self._sensor_name self._attr_device_class = self._sensor_name self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_value: Optional[ - datetime.datetime - ] = parse_air_quality_from_coordinator(coordinator, entity_id, instance) - self._attr_native_unit_of_measurement: Optional[ - str - ] = ALEXA_UNIT_CONVERSION.get(unit) + self._attr_native_value: Optional[datetime.datetime] = ( + parse_air_quality_from_coordinator(coordinator, entity_id, instance) + ) + self._attr_native_unit_of_measurement: Optional[str] = ( + ALEXA_UNIT_CONVERSION.get(unit) + ) self._attr_unique_id = entity_id + " " + self._sensor_name self._attr_icon = ALEXA_ICON_CONVERSION.get(sensor_name, ALEXA_ICON_DEFAULT) self._attr_device_info = ( @@ -447,11 +447,11 @@ def _fix_alarm_date_time(self, value): ): return value naive_time = dt.parse_datetime(value[1][self._sensor_property]) - timezone = pytz.timezone( + timezone = dt.get_time_zone( self._client._timezone # pylint: disable=protected-access ) if timezone and naive_time: - value[1][self._sensor_property] = timezone.localize(naive_time) + value[1][self._sensor_property] = naive_time.replace(tzinfo=timezone) elif not naive_time: # this is typically an older alarm value[1][self._sensor_property] = datetime.datetime.fromtimestamp( @@ -670,9 +670,11 @@ def __init__(self, client, n_json, account): "remainingTime", account, f"next {self._type}", - "mdi:timer-outline" - if (version.parse(HA_VERSION) >= version.parse("0.113.0")) - else "mdi:timer", + ( + "mdi:timer-outline" + if (version.parse(HA_VERSION) >= version.parse("0.113.0")) + else "mdi:timer" + ), ) def _process_state(self, value) -> Optional[datetime.datetime]: