1+ import logging
2+
13from bidict import bidict
2- from homeassistant .components .media_player import MediaPlayerEntity , MediaPlayerEntityFeature , MediaPlayerState
4+ from homeassistant .components .media_player import (
5+ MediaPlayerDeviceClass ,
6+ MediaPlayerEntity ,
7+ MediaPlayerEntityFeature ,
8+ MediaPlayerState ,
9+ )
310from homeassistant .config_entries import ConfigEntry
11+ from homeassistant .core import HomeAssistant
412from homeassistant .helpers .entity import DeviceInfo
513from pyextron import DeviceType , ExtronDevice , HDMISwitcher , SurroundSoundProcessor
614
715from custom_components .extron import DeviceInformation , ExtronConfigEntryRuntimeData
816from custom_components .extron .const import CONF_DEVICE_TYPE
917
18+ logger = logging .getLogger (__name__ )
19+
1020
1121def make_source_bidict (num_sources : int , input_names : list [str ]) -> bidict :
1222 # Use user-defined input name for the source when available
1323 return bidict ({i + 1 : input_names [i ] if i < len (input_names ) else str (i + 1 ) for i in range (num_sources )})
1424
1525
16- async def async_setup_entry (hass , entry : ConfigEntry , async_add_entities ):
26+ async def async_setup_entry (_hass : HomeAssistant , entry : ConfigEntry , async_add_entities ):
1727 # Extract stored runtime data from the entry
1828 runtime_data : ExtronConfigEntryRuntimeData = entry .runtime_data
1929 device = runtime_data .device
@@ -34,7 +44,7 @@ def __init__(self, device: ExtronDevice, device_information: DeviceInformation,
3444 self ._device = device
3545 self ._device_information = device_information
3646 self ._input_names = input_names
37- self ._device_class = "receiver"
47+ self ._device_class = MediaPlayerDeviceClass . RECEIVER
3848 self ._state = MediaPlayerState .PLAYING
3949
4050 def get_device_type (self ):
@@ -86,11 +96,13 @@ def get_device_type(self):
8696
8797 async def async_update (self ):
8898 try :
89- self ._source = self ._source_bidict .get (await self ._ssp .view_input ())
90- self ._muted = await self ._ssp .is_muted ()
91- volume = await self ._ssp .get_volume_level ()
92- self ._volume = volume / 100
99+ async with self ._ssp ._device .connection ():
100+ self ._source = self ._source_bidict .get (await self ._ssp .view_input ())
101+ self ._muted = await self ._ssp .is_muted ()
102+ volume = await self ._ssp .get_volume_level ()
103+ self ._volume = volume / 100
93104 except Exception :
105+ logger .exception ("An error occurred while trying to update entity state" )
94106 self ._attr_available = False
95107 else :
96108 self ._attr_available = True
@@ -119,21 +131,26 @@ def create_source_bidict(self) -> bidict:
119131 return make_source_bidict (5 , self ._input_names )
120132
121133 async def async_select_source (self , source ):
122- await self ._ssp .select_input (self ._source_bidict .inverse .get (source ))
134+ async with self ._ssp ._device .connection ():
135+ await self ._ssp .select_input (self ._source_bidict .inverse .get (source ))
123136
124137 async def async_mute_volume (self , mute : bool ) -> None :
125- await self ._ssp .mute () if mute else await self ._ssp .unmute ()
138+ async with self ._ssp ._device .connection ():
139+ await self ._ssp .mute () if mute else await self ._ssp .unmute ()
126140
127141 async def async_set_volume_level (self , volume : float ) -> None :
128- await self ._ssp .set_volume_level (int (volume * 100 ))
142+ async with self ._ssp ._device .connection ():
143+ await self ._ssp .set_volume_level (int (volume * 100 ))
129144
130145 async def async_volume_up (self ) -> None :
131- if int (self ._volume * 100 ) < 100 :
132- await self ._ssp .increment_volume ()
146+ async with self ._ssp ._device .connection ():
147+ if int (self ._volume * 100 ) < 100 :
148+ await self ._ssp .increment_volume ()
133149
134150 async def async_volume_down (self ) -> None :
135- if int (self ._volume * 100 ) > 0 :
136- await self ._ssp .decrement_volume ()
151+ async with self ._ssp ._device .connection ():
152+ if int (self ._volume * 100 ) > 0 :
153+ await self ._ssp .decrement_volume ()
137154
138155
139156class ExtronHDMISwitcher (AbstractExtronMediaPlayerEntity ):
@@ -154,8 +171,10 @@ def get_device_type(self):
154171
155172 async def async_update (self ):
156173 try :
157- self ._source = self ._source_bidict .get (await self ._hdmi_switcher .view_input ())
174+ async with self ._hdmi_switcher ._device .connection ():
175+ self ._source = self ._source_bidict .get (await self ._hdmi_switcher .view_input ())
158176 except Exception :
177+ logger .exception ("An error occurred while trying to update entity state" )
159178 self ._attr_available = False
160179 else :
161180 self ._attr_available = True
0 commit comments