From f191c548482446a47ce6ab6b78c9a6306ae650ca Mon Sep 17 00:00:00 2001 From: Oliver <932481+tb-killa@users.noreply.github.com> Date: Sat, 8 Jun 2024 06:51:30 +0200 Subject: [PATCH 1/3] fix: add hide_serial for Debug Output (#2279) closes #2255 --- custom_components/alexa_media/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/alexa_media/__init__.py b/custom_components/alexa_media/__init__.py index f31cd168..911078fd 100644 --- a/custom_components/alexa_media/__init__.py +++ b/custom_components/alexa_media/__init__.py @@ -1098,7 +1098,7 @@ async def http2_handler(message_obj): hass.data[DATA_ALEXAMEDIA]["accounts"][email]["excluded"].keys() ) ): - _LOGGER.debug("Discovered new media_player %s", serial) + _LOGGER.debug("Discovered new media_player %s", hide_serial(serial)) ( hass.data[DATA_ALEXAMEDIA]["accounts"][email]["new_devices"] ) = True From 88d38713121d428e5a2e573065563718a64029a2 Mon Sep 17 00:00:00 2001 From: Oliver <932481+tb-killa@users.noreply.github.com> Date: Sat, 8 Jun 2024 06:53:12 +0200 Subject: [PATCH 2/3] fix: swap "async_write_ha_state" with "schedule_update_ha_state" (#2280) closes #2256 --- custom_components/alexa_media/light.py | 2 +- custom_components/alexa_media/media_player.py | 28 +++++++++---------- custom_components/alexa_media/sensor.py | 2 +- custom_components/alexa_media/switch.py | 12 ++++---- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/custom_components/alexa_media/light.py b/custom_components/alexa_media/light.py index b7ddff31..39e889be 100644 --- a/custom_components/alexa_media/light.py +++ b/custom_components/alexa_media/light.py @@ -271,7 +271,7 @@ async def _set_state(self, power_on, brightness=None, kelvin=None, hs_color=None self._requested_state_at = datetime.datetime.now( datetime.timezone.utc ) # must be set last so that previous getters work properly - self.async_write_ha_state() + self.schedule_update_ha_state() async def async_turn_on(self, **kwargs): """Turn on.""" diff --git a/custom_components/alexa_media/media_player.py b/custom_components/alexa_media/media_player.py index db06c9dd..366f4be8 100644 --- a/custom_components/alexa_media/media_player.py +++ b/custom_components/alexa_media/media_player.py @@ -389,7 +389,7 @@ async def _refresh_if_no_audiopush(already_refreshed=False): return if event_serial == self.device_serial_number: self._available = True - self.async_write_ha_state() + self.schedule_update_ha_state() if "last_called_change" in event: if ( event_serial == self.device_serial_number @@ -409,8 +409,8 @@ async def _refresh_if_no_audiopush(already_refreshed=False): self._last_called = True self._last_called_timestamp = event["last_called_change"]["timestamp"] self._last_called_summary = event["last_called_change"].get("summary") - if self.hass and self.async_write_ha_state: - self.async_write_ha_state() + if self.hass and self.schedule_update_ha_state: + self.schedule_update_ha_state() await self._update_notify_targets() else: self._last_called = False @@ -437,8 +437,8 @@ async def _refresh_if_no_audiopush(already_refreshed=False): self._source_list = self._get_source_list() self._connected_bluetooth = self._get_connected_bluetooth() self._bluetooth_list = self._get_bluetooth_list() - if self.hass and self.async_write_ha_state: - self.async_write_ha_state() + if self.hass and self.schedule_update_ha_state: + self.schedule_update_ha_state() elif "player_state" in event: player_state = event["player_state"] if event_serial == self.device_serial_number: @@ -470,12 +470,12 @@ async def _refresh_if_no_audiopush(already_refreshed=False): player_state["volumeSetting"], ) self._media_vol_level = player_state["volumeSetting"] / 100 - if self.hass and self.async_write_ha_state: - self.async_write_ha_state() + if self.hass and self.schedule_update_ha_state: + self.schedule_update_ha_state() elif "dopplerConnectionState" in player_state: self.available = player_state["dopplerConnectionState"] == "ONLINE" - if self.hass and self.async_write_ha_state: - self.async_write_ha_state() + if self.hass and self.schedule_update_ha_state: + self.schedule_update_ha_state() await _refresh_if_no_audiopush(already_refreshed) elif "push_activity" in event: if self.state in {MediaPlayerState.IDLE, MediaPlayerState.PAUSED, MediaPlayerState.PLAYING}: @@ -592,7 +592,7 @@ async def refresh(self, device=None, skip_api: bool = False): ]["last_called"].get("summary") await self._update_notify_targets() if skip_api and self.hass: - self.async_write_ha_state() + self.schedule_update_ha_state() return if "MUSIC_SKILL" in self._capabilities: if self._parent_clusters and self.hass: @@ -734,7 +734,7 @@ async def refresh(self, device=None, skip_api: bool = False): ) ) if self.hass: - self.async_write_ha_state() + self.schedule_update_ha_state() @property def source(self): @@ -989,7 +989,7 @@ async def async_update(self): self.name, ) self._last_update = util.utcnow() - self.async_write_ha_state() + self.schedule_update_ha_state() @property def media_content_type(self): @@ -1074,7 +1074,7 @@ def shuffle(self): def shuffle(self, state): """Set the Shuffle state.""" self._shuffle = state - self.async_write_ha_state() + self.schedule_update_ha_state() @property def repeat_state(self): @@ -1085,7 +1085,7 @@ def repeat_state(self): def repeat_state(self, state): """Set the Repeat state.""" self._repeat = state - self.async_write_ha_state() + self.schedule_update_ha_state() @property def supported_features(self): diff --git a/custom_components/alexa_media/sensor.py b/custom_components/alexa_media/sensor.py index 0bb552c7..a49e7859 100644 --- a/custom_components/alexa_media/sensor.py +++ b/custom_components/alexa_media/sensor.py @@ -610,7 +610,7 @@ async def async_update(self): self._n_dict = None self._process_raw_notifications() try: - self.async_write_ha_state() + self.schedule_update_ha_state() except NoEntitySpecifiedError: pass # we ignore this due to a harmless startup race condition diff --git a/custom_components/alexa_media/switch.py b/custom_components/alexa_media/switch.py index a462596e..6c35f1cd 100644 --- a/custom_components/alexa_media/switch.py +++ b/custom_components/alexa_media/switch.py @@ -214,7 +214,7 @@ def _handle_event(self, event): if "queue_state" in event: queue_state = event["queue_state"] if queue_state["dopplerId"]["deviceSerialNumber"] == self._client.unique_id: - self.async_write_ha_state() + self.schedule_update_ha_state() @_catch_login_errors async def _set_switch(self, state, **kwargs): @@ -233,7 +233,7 @@ async def _set_switch(self, state, **kwargs): self.name, getattr(self._client, self._switch_property), ) - self.async_write_ha_state() + self.schedule_update_ha_state() elif self.should_poll: # if we need to poll, refresh media_client _LOGGER.debug( @@ -304,7 +304,7 @@ async def async_update(self): except AttributeError: pass try: - self.async_write_ha_state() + self.schedule_update_ha_state() except NoEntitySpecifiedError: pass # we ignore this due to a harmless startup race condition @@ -368,7 +368,7 @@ def _handle_event(self, event): if state != self.is_on: _LOGGER.debug("Detected %s changed to %s", self, state) setattr(self._client, self._switch_property, state) - self.async_write_ha_state() + self.schedule_update_ha_state() class ShuffleSwitch(AlexaMediaSwitch): @@ -467,7 +467,7 @@ async def _set_state(self, power_on): self._requested_state_at = datetime.datetime.now( datetime.timezone.utc ) # must be set last so that previous getters work properly - self.async_write_ha_state() + self.schedule_update_ha_state() async def async_turn_on(self, **kwargs): """Turn on.""" @@ -475,4 +475,4 @@ async def async_turn_on(self, **kwargs): async def async_turn_off(self, **kwargs): # pylint:disable=unused-argument """Turn off.""" - await self._set_state(False) \ No newline at end of file + await self._set_state(False) From 7cb44b20b6249336b01628e290ee943053a3fe1d Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Sat, 8 Jun 2024 02:57:58 -0400 Subject: [PATCH 3/3] 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]: