Skip to content

Commit

Permalink
Discover new devices at runtime in onewire (home-assistant#135199)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jan 10, 2025
1 parent 24c70ca commit 475a2fb
Show file tree
Hide file tree
Showing 12 changed files with 299 additions and 45 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/onewire/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: OneWireConfigEntry) -> b

await hass.config_entries.async_forward_entry_setups(entry, _PLATFORMS)

onewire_hub.schedule_scan_for_new_devices()

entry.async_on_unload(entry.add_update_listener(options_update_listener))

return True
Expand Down
32 changes: 25 additions & 7 deletions homeassistant/components/onewire/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireConfigEntry, OneWireHub
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
OneWireConfigEntry,
OneWireHub,
OWDeviceDescription,
)

# the library uses non-persistent connections
# and concurrent access to the bus is managed by the server
Expand Down Expand Up @@ -98,16 +104,28 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up 1-Wire platform."""
async_add_entities(get_entities(config_entry.runtime_data), True)

async def _add_entities(
hub: OneWireHub, devices: list[OWDeviceDescription]
) -> None:
"""Add 1-Wire entities for all devices."""
if not devices:
return
async_add_entities(get_entities(hub, devices), True)

def get_entities(onewire_hub: OneWireHub) -> list[OneWireBinarySensor]:
"""Get a list of entities."""
if not onewire_hub.devices:
return []
hub = config_entry.runtime_data
await _add_entities(hub, hub.devices)
config_entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_NEW_DEVICE_CONNECTED, _add_entities)
)


def get_entities(
onewire_hub: OneWireHub, devices: list[OWDeviceDescription]
) -> list[OneWireBinarySensor]:
"""Get a list of entities."""
entities: list[OneWireBinarySensor] = []
for device in onewire_hub.devices:
for device in devices:
family = device.family
device_id = device.id
device_type = device.type
Expand Down
43 changes: 40 additions & 3 deletions homeassistant/components/onewire/onewirehub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

from __future__ import annotations

from datetime import datetime, timedelta
import logging
import os

from pyownet import protocol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_VIA_DEVICE, CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.util.signal_type import SignalType

from .const import (
DEVICE_SUPPORT,
Expand All @@ -32,10 +36,15 @@
"EF": MANUFACTURER_HOBBYBOARDS,
}

_DEVICE_SCAN_INTERVAL = timedelta(minutes=5)
_LOGGER = logging.getLogger(__name__)

type OneWireConfigEntry = ConfigEntry[OneWireHub]

SIGNAL_NEW_DEVICE_CONNECTED = SignalType["OneWireHub", list[OWDeviceDescription]](
f"{DOMAIN}_new_device_connected"
)


def _is_known_device(device_family: str, device_type: str | None) -> bool:
"""Check if device family/type is known to the library."""
Expand Down Expand Up @@ -69,14 +78,42 @@ def _initialize(self) -> None:
async def initialize(self) -> None:
"""Initialize a config entry."""
await self._hass.async_add_executor_job(self._initialize)
# Populate the device registry
self._populate_device_registry(self.devices)

@callback
def _populate_device_registry(self, devices: list[OWDeviceDescription]) -> None:
"""Populate the device registry."""
device_registry = dr.async_get(self._hass)
for device in self.devices:
for device in devices:
device_registry.async_get_or_create(
config_entry_id=self._config_entry.entry_id,
**device.device_info,
)

def schedule_scan_for_new_devices(self) -> None:
"""Schedule a regular scan of the bus for new devices."""
self._config_entry.async_on_unload(
async_track_time_interval(
self._hass, self._scan_for_new_devices, _DEVICE_SCAN_INTERVAL
)
)

async def _scan_for_new_devices(self, _: datetime) -> None:
"""Scan the bus for new devices."""
devices = await self._hass.async_add_executor_job(
_discover_devices, self.owproxy
)
existing_device_ids = [device.id for device in self.devices]
new_devices = [
device for device in devices if device.id not in existing_device_ids
]
if new_devices:
self.devices.extend(new_devices)
self._populate_device_registry(new_devices)
async_dispatcher_send(
self._hass, SIGNAL_NEW_DEVICE_CONNECTED, self, new_devices
)


def _discover_devices(
owproxy: protocol._Proxy, path: str = "/", parent_id: str | None = None
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/onewire/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ rules:
comment: It doesn't make sense to override defaults
reconfiguration-flow: done
dynamic-devices:
status: todo
comment: Not yet implemented
status: done
comment: The bus is scanned for new devices at regular interval
discovery-update-info:
status: todo
comment: Under review
Expand Down
32 changes: 25 additions & 7 deletions homeassistant/components/onewire/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import READ_MODE_INT
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireConfigEntry, OneWireHub
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
OneWireConfigEntry,
OneWireHub,
OWDeviceDescription,
)

# the library uses non-persistent connections
# and concurrent access to the bus is managed by the server
Expand Down Expand Up @@ -45,17 +51,29 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up 1-Wire platform."""
async_add_entities(get_entities(config_entry.runtime_data), True)

async def _add_entities(
hub: OneWireHub, devices: list[OWDeviceDescription]
) -> None:
"""Add 1-Wire entities for all devices."""
if not devices:
return
async_add_entities(get_entities(hub, devices), True)

hub = config_entry.runtime_data
await _add_entities(hub, hub.devices)
config_entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_NEW_DEVICE_CONNECTED, _add_entities)
)

def get_entities(onewire_hub: OneWireHub) -> list[OneWireSelectEntity]:
"""Get a list of entities."""
if not onewire_hub.devices:
return []

def get_entities(
onewire_hub: OneWireHub, devices: list[OWDeviceDescription]
) -> list[OneWireSelectEntity]:
"""Get a list of entities."""
entities: list[OneWireSelectEntity] = []

for device in onewire_hub.devices:
for device in devices:
family = device.family
device_id = device.id
device_info = device.device_info
Expand Down
40 changes: 29 additions & 11 deletions homeassistant/components/onewire/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType

Expand All @@ -39,7 +40,12 @@
READ_MODE_INT,
)
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireConfigEntry, OneWireHub
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
OneWireConfigEntry,
OneWireHub,
OWDeviceDescription,
)

# the library uses non-persistent connections
# and concurrent access to the bus is managed by the server
Expand Down Expand Up @@ -357,23 +363,35 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up 1-Wire platform."""
# note: we have to go through the executor as SENSOR platform
# makes extra calls to the hub during device listing
entities = await hass.async_add_executor_job(
get_entities, config_entry.runtime_data, config_entry.options

async def _add_entities(
hub: OneWireHub, devices: list[OWDeviceDescription]
) -> None:
"""Add 1-Wire entities for all devices."""
if not devices:
return
# note: we have to go through the executor as SENSOR platform
# makes extra calls to the hub during device listing
entities = await hass.async_add_executor_job(
get_entities, hub, devices, config_entry.options
)
async_add_entities(entities, True)

hub = config_entry.runtime_data
await _add_entities(hub, hub.devices)
config_entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_NEW_DEVICE_CONNECTED, _add_entities)
)
async_add_entities(entities, True)


def get_entities(
onewire_hub: OneWireHub, options: MappingProxyType[str, Any]
onewire_hub: OneWireHub,
devices: list[OWDeviceDescription],
options: MappingProxyType[str, Any],
) -> list[OneWireSensor]:
"""Get a list of entities."""
if not onewire_hub.devices:
return []

entities: list[OneWireSensor] = []
for device in onewire_hub.devices:
for device in devices:
family = device.family
device_type = device.type
device_id = device.id
Expand Down
32 changes: 25 additions & 7 deletions homeassistant/components/onewire/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DEVICE_KEYS_0_3, DEVICE_KEYS_0_7, DEVICE_KEYS_A_B, READ_MODE_BOOL
from .entity import OneWireEntity, OneWireEntityDescription
from .onewirehub import OneWireConfigEntry, OneWireHub
from .onewirehub import (
SIGNAL_NEW_DEVICE_CONNECTED,
OneWireConfigEntry,
OneWireHub,
OWDeviceDescription,
)

# the library uses non-persistent connections
# and concurrent access to the bus is managed by the server
Expand Down Expand Up @@ -158,17 +164,29 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up 1-Wire platform."""
async_add_entities(get_entities(config_entry.runtime_data), True)

async def _add_entities(
hub: OneWireHub, devices: list[OWDeviceDescription]
) -> None:
"""Add 1-Wire entities for all devices."""
if not devices:
return
async_add_entities(get_entities(hub, devices), True)

hub = config_entry.runtime_data
await _add_entities(hub, hub.devices)
config_entry.async_on_unload(
async_dispatcher_connect(hass, SIGNAL_NEW_DEVICE_CONNECTED, _add_entities)
)

def get_entities(onewire_hub: OneWireHub) -> list[OneWireSwitch]:
"""Get a list of entities."""
if not onewire_hub.devices:
return []

def get_entities(
onewire_hub: OneWireHub, devices: list[OWDeviceDescription]
) -> list[OneWireSwitch]:
"""Get a list of entities."""
entities: list[OneWireSwitch] = []

for device in onewire_hub.devices:
for device in devices:
family = device.family
device_type = device.type
device_id = device.id
Expand Down
33 changes: 31 additions & 2 deletions tests/components/onewire/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
from collections.abc import Generator
from unittest.mock import MagicMock, patch

from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion

from homeassistant.components.onewire.onewirehub import _DEVICE_SCAN_INTERVAL
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

from . import setup_owproxy_mock_devices
from .const import MOCK_OWPROXY_DEVICES

from tests.common import MockConfigEntry, snapshot_platform
from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform


@pytest.fixture(autouse=True)
Expand All @@ -31,8 +33,35 @@ async def test_binary_sensors(
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
) -> None:
"""Test for 1-Wire binary sensors."""
"""Test for 1-Wire binary sensor entities."""
setup_owproxy_mock_devices(owproxy, MOCK_OWPROXY_DEVICES.keys())
await hass.config_entries.async_setup(config_entry.entry_id)

await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id)


@pytest.mark.parametrize("device_id", ["29.111111111111"])
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_binary_sensors_delayed(
hass: HomeAssistant,
config_entry: MockConfigEntry,
owproxy: MagicMock,
device_id: str,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test for delayed 1-Wire binary sensor entities."""
setup_owproxy_mock_devices(owproxy, [])
await hass.config_entries.async_setup(config_entry.entry_id)

assert not er.async_entries_for_config_entry(entity_registry, config_entry.entry_id)

setup_owproxy_mock_devices(owproxy, [device_id])
freezer.tick(_DEVICE_SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)

assert (
len(er.async_entries_for_config_entry(entity_registry, config_entry.entry_id))
== 8
)
Loading

0 comments on commit 475a2fb

Please sign in to comment.