diff --git a/.github/workflows/validate.yaml b/.github/workflows/validate.yaml index 20dc7526..df644ce9 100644 --- a/.github/workflows/validate.yaml +++ b/.github/workflows/validate.yaml @@ -83,7 +83,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.13", "3.14"] # If 3.13-tests start failing, we need to consider setting HA2026.3 as the minimum version. + python-version: ["3.14"] steps: - name: Checkout the repository uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 @@ -93,11 +93,6 @@ jobs: with: python-version: ${{ matrix.python-version }} - - name: Revert HA requirement to last version supporting Python ${{ matrix.python-version }} - if: matrix.python-version == '3.13' - run: | - sed -i 's/^homeassistant==.*/homeassistant==2026.2.3/' requirements.txt - - name: Install requirements run: | pip install \ diff --git a/README.md b/README.md index 98b20e1a..8a276b17 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Confirmed to work with Zaptec products # Requirements -* Home Assistant 2025.7 or newer. +* Home Assistant 2026.8 or newer. * A user with access to [Zaptec Portal](https://portal.zaptec.com/). * If you want to use the controls at the [Installation](#zaptec-device-concept) level: * A Zaptec Portal user with _Owner_ or _Service_ privileges. diff --git a/custom_components/zaptec/manager.py b/custom_components/zaptec/manager.py index c996a45c..9d72d5ac 100644 --- a/custom_components/zaptec/manager.py +++ b/custom_components/zaptec/manager.py @@ -15,6 +15,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.entity import DeviceInfo, EntityDescription from homeassistant.util.ssl import get_default_context @@ -155,6 +156,15 @@ def all_coordinators(self) -> Iterable[ZaptecUpdateCoordinator]: *self.device_coordinators.values(), ] + def _common_device_info(self, zaptec_obj: ZaptecBase) -> DeviceInfo: + """Return the device info shared by all entities of a Zaptec object.""" + return DeviceInfo( + manufacturer=MANUFACTURER, + identifiers={(DOMAIN, zaptec_obj.id)}, + model=zaptec_obj.model, + name=self.name_prefix + zaptec_obj.name, + ) + def create_entities_from_descriptions( self, descriptions: Iterable[ZaptecEntityDescription], @@ -164,12 +174,7 @@ def create_entities_from_descriptions( """Create entities from EntityDescription objects.""" # Start with the common device info and append the provided device info - dev_info = DeviceInfo( - manufacturer=MANUFACTURER, - identifiers={(DOMAIN, zaptec_obj.id)}, - model=zaptec_obj.model, - name=self.name_prefix + zaptec_obj.name, - ) + dev_info = self._common_device_info(zaptec_obj) dev_info.update(device_info) entities: list[ZaptecBaseEntity] = [] @@ -246,8 +251,18 @@ def create_entities_from_zaptec( elif isinstance(obj, Charger): info = DeviceInfo() - if obj.installation: - info["via_device"] = (DOMAIN, obj.installation.id) + if ( + obj.id in self.tracked_devices + and obj.installation is not None + and obj.installation.id in self.tracked_devices + ): + # via_device_id needs the installation device to exist + # before the charger entities are added + device = dr.async_get(self.hass).async_get_or_create( + config_entry_id=self.config_entry.entry_id, + **self._common_device_info(obj.installation), + ) + info["via_device_id"] = device.id entities.extend( self.create_entities_from_descriptions( diff --git a/hacs.json b/hacs.json index 40a0ea62..ca8b8745 100644 --- a/hacs.json +++ b/hacs.json @@ -1,5 +1,5 @@ { "name": "Zaptec EV charger", - "homeassistant": "2025.7.0", + "homeassistant": "2026.8.0", "hacs": "2.0.5" } \ No newline at end of file diff --git a/tests/test_manager.py b/tests/test_manager.py index 014d6bfd..41b2497f 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -7,18 +7,21 @@ import logging from types import SimpleNamespace from typing import Any -from unittest.mock import AsyncMock +from unittest.mock import AsyncMock, MagicMock import pytest from custom_components.zaptec import manager as manager_module +from custom_components.zaptec.const import DOMAIN, MANUFACTURER from custom_components.zaptec.manager import ( STREAM_RECONNECT_INIT_DELAY, STREAM_RECONNECT_MAX_DELAY, STREAM_RECONNECT_STABLE_TIME, + ZaptecEntityDescription, ZaptecManager, _stream_supervisor, ) +from custom_components.zaptec.zaptec import Charger, Installation CHARGER_ID = "e6f5a1b2-0000-4000-8000-000000000001" @@ -353,6 +356,68 @@ async def stream_main(*, on_connect: Callable[[], None], **kwargs: object) -> No assert len(warnings) == 1 # the second failure never connected, so it is the same outage +class FakeEntity: + """Records the device info an entity is created with.""" + + def __init__(self, *, device_info: dict[str, Any], **kwargs: object) -> None: + """Store the device info.""" + self.device_info = device_info + + +def _device_link_setup( + monkeypatch: pytest.MonkeyPatch, tracked: set[str] +) -> tuple[ZaptecManager, Installation, MagicMock]: + """Return a manager with one installation and charger, and a fake device registry.""" + zaptec = SimpleNamespace(redact=lambda v, **_: v, show_all_updates=False) + install = Installation({"Id": "inst-1", "Name": "Home"}, zaptec) # type: ignore[arg-type] + charger = Charger({"Id": CHARGER_ID, "Name": "Garage"}, zaptec, installation=install) # type: ignore[arg-type] + zaptec.objects = lambda: [install, charger] + + registry = MagicMock() + registry.async_get_or_create.return_value = SimpleNamespace(id="install-device-id") + monkeypatch.setattr(manager_module.dr, "async_get", lambda _hass: registry) + + manager = ZaptecManager( + None, # type: ignore[arg-type] + entry=SimpleNamespace(entry_id="entry-1"), # type: ignore[arg-type] + zaptec=zaptec, # type: ignore[arg-type] + tracked_devices=tracked, + ) + manager.device_coordinators = {install.id: None, CHARGER_ID: None} # type: ignore[dict-item] + return manager, install, registry + + +def test_charger_device_links_to_installation_device(monkeypatch: pytest.MonkeyPatch) -> None: + """Chargers point at the installation's device registry id, not its identifiers.""" + manager, install, registry = _device_link_setup(monkeypatch, {"inst-1", CHARGER_ID}) + description = ZaptecEntityDescription(key="test", cls=FakeEntity) # type: ignore[arg-type] + + _, charger_entity = manager.create_entities_from_zaptec([description], [description]) + + registry.async_get_or_create.assert_called_once_with( + config_entry_id="entry-1", + manufacturer=MANUFACTURER, + identifiers={(DOMAIN, install.id)}, + model=install.model, + name="Home", + ) + assert charger_entity.device_info["via_device_id"] == "install-device-id" + assert "via_device" not in charger_entity.device_info + + +def test_charger_device_without_tracked_installation_has_no_link( + monkeypatch: pytest.MonkeyPatch, +) -> None: + """An untracked installation gets no device, so the charger is not linked to one.""" + manager, _, registry = _device_link_setup(monkeypatch, {CHARGER_ID}) + description = ZaptecEntityDescription(key="test", cls=FakeEntity) # type: ignore[arg-type] + + (charger_entity,) = manager.create_entities_from_zaptec([description], [description]) + + registry.async_get_or_create.assert_not_called() + assert "via_device_id" not in charger_entity.device_info + + class FakeZaptec: """Stands in for the Zaptec account hierarchy built by first_time_setup."""