Skip to content

Commit e9ff1b0

Browse files
authored
Merge pull request #21 from NitorCreations/connection-less
Update pyextron to 0.2.0, switch to connection-less
2 parents 43a9008 + 67123e9 commit e9ff1b0

File tree

6 files changed

+52
-33
lines changed

6 files changed

+52
-33
lines changed

custom_components/extron/__init__.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ class ExtronConfigEntryRuntimeData:
3232

3333

3434
async def get_device_information(device: ExtronDevice) -> DeviceInformation:
35-
mac_address = await device.query_mac_address()
36-
model_name = await device.query_model_name()
37-
firmware_version = await device.query_firmware_version()
38-
part_number = await device.query_part_number()
39-
ip_address = await device.query_ip_address()
35+
async with device.connection():
36+
mac_address = await device.query_mac_address()
37+
model_name = await device.query_model_name()
38+
firmware_version = await device.query_firmware_version()
39+
part_number = await device.query_part_number()
40+
ip_address = await device.query_ip_address()
4041

4142
device_info = DeviceInfo(
4243
identifiers={(DOMAIN, format_mac(mac_address))},
@@ -57,6 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5758
try:
5859
device = ExtronDevice(entry.data["host"], entry.data["port"], entry.data["password"])
5960
await device.connect()
61+
await device.disconnect()
6062
except AuthenticationError as e:
6163
raise ConfigEntryNotReady("Invalid credentials") from e
6264
except Exception as e:

custom_components/extron/button.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import logging
2-
31
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
42
from homeassistant.config_entries import ConfigEntry
3+
from homeassistant.core import HomeAssistant
54
from homeassistant.helpers.device_registry import DeviceInfo
5+
from pyextron import ExtronDevice
66

7-
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData, ExtronDevice
8-
9-
logger = logging.getLogger(__name__)
7+
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
108

119

12-
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities):
10+
async def async_setup_entry(_hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
1311
# Extract stored runtime data from the entry
1412
runtime_data: ExtronConfigEntryRuntimeData = entry.runtime_data
1513
device = runtime_data.device
@@ -39,7 +37,5 @@ def name(self):
3937
return f"Extron {self._device_information.model_name} reboot button"
4038

4139
async def async_press(self) -> None:
42-
await self._device.reboot()
43-
44-
# Disconnect immediately so we start attempting to reconnect immediately
45-
await self._device.disconnect()
40+
async with self._device.connection():
41+
await self._device.reboot()

custom_components/extron/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"iot_class": "local_polling",
1414
"requirements": [
1515
"bidict",
16-
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@e8b516f"
16+
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@0.2.0"
1717
],
1818
"ssdp": [],
1919
"zeroconf": [],

custom_components/extron/media_player.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1+
import logging
2+
13
from 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+
)
310
from homeassistant.config_entries import ConfigEntry
11+
from homeassistant.core import HomeAssistant
412
from homeassistant.helpers.entity import DeviceInfo
513
from pyextron import DeviceType, ExtronDevice, HDMISwitcher, SurroundSoundProcessor
614

715
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
816
from custom_components.extron.const import CONF_DEVICE_TYPE
917

18+
logger = logging.getLogger(__name__)
19+
1020

1121
def 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

139156
class 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

custom_components/extron/sensor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ def native_value(self) -> StateType | date | datetime | Decimal:
5656

5757
async def async_update(self):
5858
try:
59-
self._native_value = await self._ssp.get_temperature()
59+
async with self._ssp._device.connection():
60+
self._native_value = await self._ssp.get_temperature()
6061
except Exception:
62+
logger.exception(f"async_update from {self.name} encountered error:")
6163
self._attr_available = False
6264
else:
6365
self._attr_available = True

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires-python = ">=3.12"
88
dependencies = [
99
"homeassistant",
1010
"bidict",
11-
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@e8b516f"
11+
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@0.2.0"
1212
]
1313

1414
[tool.black]

0 commit comments

Comments
 (0)