Do not pass hass.data to Sonos entities (#49881)

This commit is contained in:
jjlawren 2021-04-30 00:01:09 -05:00 committed by GitHub
parent 73714eba4b
commit adba82de8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 15 deletions

View File

@ -11,7 +11,7 @@ from homeassistant.components.binary_sensor import (
) )
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DATA_SONOS, SONOS_CREATE_BATTERY from .const import SONOS_CREATE_BATTERY
from .entity import SonosSensorEntity from .entity import SonosSensorEntity
from .speaker import SonosSpeaker from .speaker import SonosSpeaker
@ -24,7 +24,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Sonos from a config entry.""" """Set up Sonos from a config entry."""
async def _async_create_entity(speaker: SonosSpeaker) -> None: async def _async_create_entity(speaker: SonosSpeaker) -> None:
entity = SonosPowerEntity(speaker, hass.data[DATA_SONOS]) entity = SonosPowerEntity(speaker)
async_add_entities([entity]) async_add_entities([entity])
config_entry.async_on_unload( config_entry.async_on_unload(

View File

@ -13,7 +13,6 @@ from homeassistant.helpers.dispatcher import (
) )
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from . import SonosData
from .const import ( from .const import (
DOMAIN, DOMAIN,
SONOS_ENTITY_CREATED, SONOS_ENTITY_CREATED,
@ -28,10 +27,9 @@ _LOGGER = logging.getLogger(__name__)
class SonosEntity(Entity): class SonosEntity(Entity):
"""Representation of a Sonos entity.""" """Representation of a Sonos entity."""
def __init__(self, speaker: SonosSpeaker, sonos_data: SonosData) -> None: def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize a SonosEntity.""" """Initialize a SonosEntity."""
self.speaker = speaker self.speaker = speaker
self.data = sonos_data
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Handle common setup when added to hass.""" """Handle common setup when added to hass."""

View File

@ -68,7 +68,6 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.network import is_internal_request from homeassistant.helpers.network import is_internal_request
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
from . import SonosData
from .const import ( from .const import (
DATA_SONOS, DATA_SONOS,
DOMAIN as SONOS_DOMAIN, DOMAIN as SONOS_DOMAIN,
@ -157,7 +156,7 @@ async def async_setup_entry(
@callback @callback
def async_create_entities(speaker: SonosSpeaker) -> None: def async_create_entities(speaker: SonosSpeaker) -> None:
"""Handle device discovery and create entities.""" """Handle device discovery and create entities."""
async_add_entities([SonosMediaPlayerEntity(speaker, hass.data[DATA_SONOS])]) async_add_entities([SonosMediaPlayerEntity(speaker)])
@service.verify_domain_control(hass, SONOS_DOMAIN) @service.verify_domain_control(hass, SONOS_DOMAIN)
async def async_service_handle(service_call: ServiceCall) -> None: async def async_service_handle(service_call: ServiceCall) -> None:
@ -322,9 +321,9 @@ def _timespan_secs(timespan: str | None) -> None | float:
class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity): class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
"""Representation of a Sonos entity.""" """Representation of a Sonos entity."""
def __init__(self, speaker: SonosSpeaker, sonos_data: SonosData) -> None: def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize the Sonos entity.""" """Initialize the Sonos entity."""
super().__init__(speaker, sonos_data) super().__init__(speaker)
self._volume_increment = 2 self._volume_increment = 2
self._player_volume: int | None = None self._player_volume: int | None = None
self._player_muted: bool | None = None self._player_muted: bool | None = None
@ -352,7 +351,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Subscribe sonos events.""" """Subscribe sonos events."""
self.data.media_player_entities[self.unique_id] = self self.hass.data[DATA_SONOS].media_player_entities[self.unique_id] = self
await self.async_reconnect_player() await self.async_reconnect_player()
await super().async_added_to_hass() await super().async_added_to_hass()
@ -542,7 +541,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
self.schedule_update_ha_state() self.schedule_update_ha_state()
# Also update slaves # Also update slaves
entities = self.data.media_player_entities.values() entities = self.hass.data[DATA_SONOS].media_player_entities.values()
for entity in entities: for entity in entities:
coordinator = entity.coordinator coordinator = entity.coordinator
if coordinator and coordinator.unique_id == self.unique_id: if coordinator and coordinator.unique_id == self.unique_id:
@ -724,13 +723,13 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
async def _async_handle_group_event(event: SonosEvent) -> None: async def _async_handle_group_event(event: SonosEvent) -> None:
"""Get async lock and handle event.""" """Get async lock and handle event."""
async with self.data.topology_condition: async with self.hass.data[DATA_SONOS].topology_condition:
group = await _async_extract_group(event) group = await _async_extract_group(event)
if self.unique_id == group[0]: if self.unique_id == group[0]:
_async_regroup(group) _async_regroup(group)
self.data.topology_condition.notify_all() self.hass.data[DATA_SONOS].topology_condition.notify_all()
if event and not hasattr(event, "zone_player_uui_ds_in_group"): if event and not hasattr(event, "zone_player_uui_ds_in_group"):
return None return None

View File

@ -8,7 +8,7 @@ from homeassistant.components.sensor import SensorEntity
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import DATA_SONOS, SONOS_CREATE_BATTERY from .const import SONOS_CREATE_BATTERY
from .entity import SonosSensorEntity from .entity import SonosSensorEntity
from .speaker import SonosSpeaker from .speaker import SonosSpeaker
@ -19,7 +19,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Sonos from a config entry.""" """Set up Sonos from a config entry."""
async def _async_create_entity(speaker: SonosSpeaker) -> None: async def _async_create_entity(speaker: SonosSpeaker) -> None:
entity = SonosBatteryEntity(speaker, hass.data[DATA_SONOS]) entity = SonosBatteryEntity(speaker)
async_add_entities([entity]) async_add_entities([entity])
config_entry.async_on_unload( config_entry.async_on_unload(