Skip to content
Open
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
7 changes: 1 addition & 6 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 23 additions & 8 deletions custom_components/zaptec/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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],
Expand All @@ -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] = []
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Zaptec EV charger",
"homeassistant": "2025.7.0",
"homeassistant": "2026.8.0",
"hacs": "2.0.5"
}
67 changes: 66 additions & 1 deletion tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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."""

Expand Down
Loading