-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
90 lines (67 loc) · 3.16 KB
/
__init__.py
File metadata and controls
90 lines (67 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""The Extron integration."""
import logging
from dataclasses import dataclass
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import DOMAIN, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import DeviceInfo, format_mac
from pyextron import AuthenticationError, ExtronDevice
from custom_components.extron.const import EXTRON_DEVICE_TIMEOUT_SECONDS, OPTION_INPUT_NAMES
PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.SENSOR, Platform.BUTTON]
_LOGGER = logging.getLogger(__name__)
@dataclass
class DeviceInformation:
mac_address: str
model_name: str
device_info: DeviceInfo
@dataclass
class ExtronConfigEntryRuntimeData:
device: ExtronDevice
device_information: DeviceInformation
input_names: list[str]
async def get_device_information(device: ExtronDevice) -> DeviceInformation:
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))},
name=f"Extron {model_name}",
manufacturer="Extron",
model=model_name,
sw_version=firmware_version,
serial_number=part_number,
configuration_url=f"http://{ip_address}/",
)
return DeviceInformation(mac_address=format_mac(mac_address), model_name=model_name, device_info=device_info)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Extron from a config entry."""
# Verify we can connect to the device
try:
device = ExtronDevice(
entry.data["host"], entry.data["port"], entry.data["password"], timeout=EXTRON_DEVICE_TIMEOUT_SECONDS
)
await device.connect()
await device.disconnect()
except AuthenticationError as e:
raise ConfigEntryNotReady("Invalid credentials") from e
except Exception as e:
raise ConfigEntryNotReady("Unable to connect") from e
# Store runtime information
device_information = await get_device_information(device)
input_names = entry.options.get(OPTION_INPUT_NAMES, [])
entry.runtime_data = ExtronConfigEntryRuntimeData(device, device_information, input_names)
# Register a listener for option updates
entry.async_on_unload(entry.add_update_listener(entry_update_listener))
_LOGGER.info(f"Initializing entry with runtime data: {entry.runtime_data}")
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def entry_update_listener(hass: HomeAssistant, config_entry: ConfigEntry):
# Reload the entry when options have been changed
await hass.config_entries.async_reload(config_entry.entry_id)