Skip to content

Commit

Permalink
Merge pull request #2282 from alandtse/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse authored Jun 8, 2024
2 parents 4ae22cd + 7cb44b2 commit 4f35b87
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion custom_components/alexa_media/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/alexa_media/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
28 changes: 14 additions & 14 deletions custom_components/alexa_media/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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}:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
34 changes: 18 additions & 16 deletions custom_components/alexa_media/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down
12 changes: 6 additions & 6 deletions custom_components/alexa_media/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -467,12 +467,12 @@ 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."""
await self._set_state(True)

async def async_turn_off(self, **kwargs): # pylint:disable=unused-argument
"""Turn off."""
await self._set_state(False)
await self._set_state(False)

0 comments on commit 4f35b87

Please sign in to comment.