1 """Hub for communication with 1-Wire server or mount_dir."""
3 from __future__
import annotations
7 from typing
import TYPE_CHECKING
9 from pyownet
import protocol
30 MANUFACTURER_HOBBYBOARDS,
33 from .model
import OWDeviceDescription
40 DEVICE_MANUFACTURER = {
41 "7E": MANUFACTURER_EDS,
42 "EF": MANUFACTURER_HOBBYBOARDS,
45 _LOGGER = logging.getLogger(__name__)
49 """Check if device family/type is known to the library."""
50 if device_family
in (
"7E",
"EF"):
51 return device_type
in DEVICE_SUPPORT[device_family]
52 return device_family
in DEVICE_SUPPORT
56 """Hub to communicate with server."""
58 def __init__(self, hass: HomeAssistant) ->
None:
61 self.
owproxyowproxy: protocol._Proxy |
None =
None
62 self.
devicesdevices: list[OWDeviceDescription] |
None =
None
64 async
def connect(self, host: str, port: int) ->
None:
65 """Connect to the server."""
67 self.
owproxyowproxy = await self.
hasshass.async_add_executor_job(
68 protocol.proxy, host, port
70 except protocol.ConnError
as exc:
71 raise CannotConnect
from exc
73 async
def initialize(self, config_entry: ConfigEntry) ->
None:
74 """Initialize a config entry."""
75 host = config_entry.data[CONF_HOST]
76 port = config_entry.data[CONF_PORT]
77 _LOGGER.debug(
"Initializing connection to %s:%s", host, port)
78 await self.
connectconnect(host, port)
83 device_registry = dr.async_get(self.
hasshass)
84 for device
in self.
devicesdevices:
85 device_info: DeviceInfo = device.device_info
86 device_registry.async_get_or_create(
87 config_entry_id=config_entry.entry_id,
88 identifiers=device_info[ATTR_IDENTIFIERS],
89 manufacturer=device_info[ATTR_MANUFACTURER],
90 model=device_info[ATTR_MODEL],
91 name=device_info[ATTR_NAME],
92 via_device=device_info.get(ATTR_VIA_DEVICE),
96 """Discover all devices."""
98 self.
devicesdevices = await self.
hasshass.async_add_executor_job(
103 self, path: str =
"/", parent_id: str |
None =
None
104 ) -> list[OWDeviceDescription]:
105 """Discover all server devices."""
106 devices: list[OWDeviceDescription] = []
108 for device_path
in self.
owproxyowproxy.dir(path):
109 device_id = os.path.split(os.path.split(device_path)[0])[1]
110 device_family = self.
owproxyowproxy.read(f
"{device_path}family").decode()
111 _LOGGER.debug(
"read `%sfamily`: %s", device_path, device_family)
115 "Ignoring unknown device family/type (%s/%s) found for device %s",
121 device_info: DeviceInfo = {
122 ATTR_IDENTIFIERS: {(DOMAIN, device_id)},
123 ATTR_MANUFACTURER: DEVICE_MANUFACTURER.get(
124 device_family, MANUFACTURER_MAXIM
126 ATTR_MODEL: device_type,
127 ATTR_NAME: device_id,
130 device_info[ATTR_VIA_DEVICE] = (DOMAIN, parent_id)
132 device_info=device_info,
134 family=device_family,
138 devices.append(device)
139 if device_branches := DEVICE_COUPLERS.get(device_family):
140 for branch
in device_branches:
142 f
"{device_path}{branch}", device_id
148 """Get device model."""
152 device_type = self.
owproxyowproxy.read(f
"{device_path}type").decode()
153 except protocol.ProtocolError
as exc:
154 _LOGGER.debug(
"Unable to read `%stype`: %s", device_path, exc)
156 _LOGGER.debug(
"read `%stype`: %s", device_path, device_type)
157 if device_type ==
"EDS":
158 device_type = self.
owproxyowproxy.read(f
"{device_path}device_type").decode()
159 _LOGGER.debug(
"read `%sdevice_type`: %s", device_path, device_type)
161 assert isinstance(device_type, str)
166 """Error to indicate we cannot connect."""
169 class InvalidPath(HomeAssistantError):
170 """Error to indicate the path is invalid."""
None connect(self, str host, int port)
str|None _get_device_type(self, str device_path)
None discover_devices(self)
None initialize(self, ConfigEntry config_entry)
list[OWDeviceDescription] _discover_devices(self, str path="/", str|None parent_id=None)
None __init__(self, HomeAssistant hass)
bool _is_known_device(str device_family, str|None device_type)