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 .const import DATA_SONOS, SONOS_CREATE_BATTERY
from .const import SONOS_CREATE_BATTERY
from .entity import SonosSensorEntity
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."""
async def _async_create_entity(speaker: SonosSpeaker) -> None:
entity = SonosPowerEntity(speaker, hass.data[DATA_SONOS])
entity = SonosPowerEntity(speaker)
async_add_entities([entity])
config_entry.async_on_unload(

View file

@ -13,7 +13,6 @@ from homeassistant.helpers.dispatcher import (
)
from homeassistant.helpers.entity import Entity
from . import SonosData
from .const import (
DOMAIN,
SONOS_ENTITY_CREATED,
@ -28,10 +27,9 @@ _LOGGER = logging.getLogger(__name__)
class SonosEntity(Entity):
"""Representation of a Sonos entity."""
def __init__(self, speaker: SonosSpeaker, sonos_data: SonosData) -> None:
def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize a SonosEntity."""
self.speaker = speaker
self.data = sonos_data
async def async_added_to_hass(self) -> None:
"""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.util.dt import utcnow
from . import SonosData
from .const import (
DATA_SONOS,
DOMAIN as SONOS_DOMAIN,
@ -157,7 +156,7 @@ async def async_setup_entry(
@callback
def async_create_entities(speaker: SonosSpeaker) -> None:
"""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)
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):
"""Representation of a Sonos entity."""
def __init__(self, speaker: SonosSpeaker, sonos_data: SonosData) -> None:
def __init__(self, speaker: SonosSpeaker) -> None:
"""Initialize the Sonos entity."""
super().__init__(speaker, sonos_data)
super().__init__(speaker)
self._volume_increment = 2
self._player_volume: int | None = None
self._player_muted: bool | None = None
@ -352,7 +351,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
async def async_added_to_hass(self) -> None:
"""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 super().async_added_to_hass()
@ -542,7 +541,7 @@ class SonosMediaPlayerEntity(SonosEntity, MediaPlayerEntity):
self.schedule_update_ha_state()
# Also update slaves
entities = self.data.media_player_entities.values()
entities = self.hass.data[DATA_SONOS].media_player_entities.values()
for entity in entities:
coordinator = entity.coordinator
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:
"""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)
if self.unique_id == group[0]:
_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"):
return None

View file

@ -8,7 +8,7 @@ from homeassistant.components.sensor import SensorEntity
from homeassistant.const import DEVICE_CLASS_BATTERY, PERCENTAGE
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 .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."""
async def _async_create_entity(speaker: SonosSpeaker) -> None:
entity = SonosBatteryEntity(speaker, hass.data[DATA_SONOS])
entity = SonosBatteryEntity(speaker)
async_add_entities([entity])
config_entry.async_on_unload(