Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions custom_components/extron/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class ExtronConfigEntryRuntimeData:


async def get_device_information(device: ExtronDevice) -> DeviceInformation:
mac_address = await device.query_mac_address()
model_name = await device.query_model_name()
firmware_version = await device.query_firmware_version()
part_number = await device.query_part_number()
ip_address = await device.query_ip_address()
async with device.connection():
mac_address = await device.query_mac_address()
model_name = await device.query_model_name()
firmware_version = await device.query_firmware_version()
part_number = await device.query_part_number()
ip_address = await device.query_ip_address()

device_info = DeviceInfo(
identifiers={(DOMAIN, format_mac(mac_address))},
Expand All @@ -57,6 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try:
device = ExtronDevice(entry.data["host"], entry.data["port"], entry.data["password"])
await device.connect()
await device.disconnect()
except AuthenticationError as e:
raise ConfigEntryNotReady("Invalid credentials") from e
except Exception as e:
Expand Down
16 changes: 6 additions & 10 deletions custom_components/extron/button.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import logging

from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceInfo
from pyextron import ExtronDevice

from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData, ExtronDevice

logger = logging.getLogger(__name__)
from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData


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

async def async_press(self) -> None:
await self._device.reboot()

# Disconnect immediately so we start attempting to reconnect immediately
await self._device.disconnect()
async with self._device.connection():
await self._device.reboot()
2 changes: 1 addition & 1 deletion custom_components/extron/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"iot_class": "local_polling",
"requirements": [
"bidict",
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@e8b516f"
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@0.2.0"
],
"ssdp": [],
"zeroconf": [],
Expand Down
49 changes: 34 additions & 15 deletions custom_components/extron/media_player.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import logging

from bidict import bidict
from homeassistant.components.media_player import MediaPlayerEntity, MediaPlayerEntityFeature, MediaPlayerState
from homeassistant.components.media_player import (
MediaPlayerDeviceClass,
MediaPlayerEntity,
MediaPlayerEntityFeature,
MediaPlayerState,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from pyextron import DeviceType, ExtronDevice, HDMISwitcher, SurroundSoundProcessor

from custom_components.extron import DeviceInformation, ExtronConfigEntryRuntimeData
from custom_components.extron.const import CONF_DEVICE_TYPE

logger = logging.getLogger(__name__)


def make_source_bidict(num_sources: int, input_names: list[str]) -> bidict:
# Use user-defined input name for the source when available
return bidict({i + 1: input_names[i] if i < len(input_names) else str(i + 1) for i in range(num_sources)})


async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities):
async def async_setup_entry(_hass: HomeAssistant, entry: ConfigEntry, async_add_entities):
# Extract stored runtime data from the entry
runtime_data: ExtronConfigEntryRuntimeData = entry.runtime_data
device = runtime_data.device
Expand All @@ -34,7 +44,7 @@ def __init__(self, device: ExtronDevice, device_information: DeviceInformation,
self._device = device
self._device_information = device_information
self._input_names = input_names
self._device_class = "receiver"
self._device_class = MediaPlayerDeviceClass.RECEIVER
self._state = MediaPlayerState.PLAYING

def get_device_type(self):
Expand Down Expand Up @@ -86,11 +96,13 @@ def get_device_type(self):

async def async_update(self):
try:
self._source = self._source_bidict.get(await self._ssp.view_input())
self._muted = await self._ssp.is_muted()
volume = await self._ssp.get_volume_level()
self._volume = volume / 100
async with self._ssp._device.connection():
self._source = self._source_bidict.get(await self._ssp.view_input())
self._muted = await self._ssp.is_muted()
volume = await self._ssp.get_volume_level()
self._volume = volume / 100
except Exception:
logger.exception("An error occurred while trying to update entity state")
self._attr_available = False
else:
self._attr_available = True
Expand Down Expand Up @@ -119,21 +131,26 @@ def create_source_bidict(self) -> bidict:
return make_source_bidict(5, self._input_names)

async def async_select_source(self, source):
await self._ssp.select_input(self._source_bidict.inverse.get(source))
async with self._ssp._device.connection():
await self._ssp.select_input(self._source_bidict.inverse.get(source))

async def async_mute_volume(self, mute: bool) -> None:
await self._ssp.mute() if mute else await self._ssp.unmute()
async with self._ssp._device.connection():
await self._ssp.mute() if mute else await self._ssp.unmute()

async def async_set_volume_level(self, volume: float) -> None:
await self._ssp.set_volume_level(int(volume * 100))
async with self._ssp._device.connection():
await self._ssp.set_volume_level(int(volume * 100))

async def async_volume_up(self) -> None:
if int(self._volume * 100) < 100:
await self._ssp.increment_volume()
async with self._ssp._device.connection():
if int(self._volume * 100) < 100:
await self._ssp.increment_volume()

async def async_volume_down(self) -> None:
if int(self._volume * 100) > 0:
await self._ssp.decrement_volume()
async with self._ssp._device.connection():
if int(self._volume * 100) > 0:
await self._ssp.decrement_volume()


class ExtronHDMISwitcher(AbstractExtronMediaPlayerEntity):
Expand All @@ -154,8 +171,10 @@ def get_device_type(self):

async def async_update(self):
try:
self._source = self._source_bidict.get(await self._hdmi_switcher.view_input())
async with self._hdmi_switcher._device.connection():
self._source = self._source_bidict.get(await self._hdmi_switcher.view_input())
except Exception:
logger.exception("An error occurred while trying to update entity state")
self._attr_available = False
else:
self._attr_available = True
Expand Down
4 changes: 3 additions & 1 deletion custom_components/extron/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ def native_value(self) -> StateType | date | datetime | Decimal:

async def async_update(self):
try:
self._native_value = await self._ssp.get_temperature()
async with self._ssp._device.connection():
self._native_value = await self._ssp.get_temperature()
except Exception:
logger.exception(f"async_update from {self.name} encountered error:")
self._attr_available = False
else:
self._attr_available = True
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ requires-python = ">=3.12"
dependencies = [
"homeassistant",
"bidict",
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@e8b516f"
"pyextron @ git+https://github.com/NitorCreations/pyextron.git@0.2.0"
]

[tool.black]
Expand Down