Compare commits

...

5 Commits

Author SHA1 Message Date
epenet
f2361ef5aa Drop ignore-missing-annotations from pylint 2026-04-07 11:25:00 +00:00
Kevin McCormack
2f0488f985 Opnsense swap to aiopnsense (#167026)
Co-authored-by: Snuffy2 <Snuffy2@users.noreply.github.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
2026-04-07 13:19:49 +02:00
Petar Petrov
61c02c854f Add websocket subscription support for calendar events (#156340)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-04-07 14:04:51 +03:00
Nelson Osacky
a10f16ce3e Add missing Miele dishwasher program ID 201 (#167536)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 12:47:48 +02:00
J. Diego Rodríguez Royo
293db47101 Bump aiohomeconnect to 0.34.0 (#167592) 2026-04-07 12:47:03 +02:00
16 changed files with 526 additions and 62 deletions

View File

@@ -709,7 +709,7 @@ jobs:
run: |
. venv/bin/activate
python --version
pylint --ignore-missing-annotations=y homeassistant
pylint homeassistant
- name: Run pylint (partially)
if: needs.info.outputs.test_full_suite == 'false'
shell: bash
@@ -718,7 +718,7 @@ jobs:
run: |
. venv/bin/activate
python --version
pylint --ignore-missing-annotations=y $(printf "homeassistant/components/%s " ${INTEGRATIONS_GLOB})
pylint $(printf "homeassistant/components/%s " ${INTEGRATIONS_GLOB})
pylint-tests:
name: Check pylint on tests

4
CODEOWNERS generated
View File

@@ -1263,8 +1263,8 @@ CLAUDE.md @home-assistant/core
/tests/components/openuv/ @bachya
/homeassistant/components/openweathermap/ @fabaff @freekode @nzapponi @wittypluck
/tests/components/openweathermap/ @fabaff @freekode @nzapponi @wittypluck
/homeassistant/components/opnsense/ @mtreinish
/tests/components/opnsense/ @mtreinish
/homeassistant/components/opnsense/ @HarlemSquirrel @Snuffy2
/tests/components/opnsense/ @HarlemSquirrel @Snuffy2
/homeassistant/components/opower/ @tronikos
/tests/components/opower/ @tronikos
/homeassistant/components/oralb/ @bdraco @Lash-L

View File

@@ -17,6 +17,7 @@ import voluptuous as vol
from homeassistant.components import frontend, http, websocket_api
from homeassistant.components.websocket_api import (
ERR_INVALID_FORMAT,
ERR_NOT_FOUND,
ERR_NOT_SUPPORTED,
ActiveConnection,
@@ -33,6 +34,7 @@ from homeassistant.core import (
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.debounce import Debouncer
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import async_track_point_in_time
@@ -76,6 +78,7 @@ ENTITY_ID_FORMAT = DOMAIN + ".{}"
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
SCAN_INTERVAL = datetime.timedelta(seconds=60)
EVENT_LISTENER_DEBOUNCE_COOLDOWN = 1.0 # seconds
# Don't support rrules more often than daily
VALID_FREQS = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY"}
@@ -320,6 +323,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
websocket_api.async_register_command(hass, handle_calendar_event_create)
websocket_api.async_register_command(hass, handle_calendar_event_delete)
websocket_api.async_register_command(hass, handle_calendar_event_update)
websocket_api.async_register_command(hass, handle_calendar_event_subscribe)
component.async_register_entity_service(
CREATE_EVENT_SERVICE,
@@ -517,6 +521,17 @@ class CalendarEntity(Entity):
_entity_component_unrecorded_attributes = frozenset({"description"})
_alarm_unsubs: list[CALLBACK_TYPE] | None = None
_event_listeners: (
list[
tuple[
datetime.datetime,
datetime.datetime,
Callable[[list[JsonValueType] | None], None],
]
]
| None
) = None
_event_listener_debouncer: Debouncer[None] | None = None
_attr_initial_color: str | None
@@ -585,6 +600,10 @@ class CalendarEntity(Entity):
the current or upcoming event.
"""
super()._async_write_ha_state()
# Notify websocket subscribers of event changes (debounced)
if self._event_listeners and self._event_listener_debouncer:
self._event_listener_debouncer.async_schedule_call()
if self._alarm_unsubs is None:
self._alarm_unsubs = []
_LOGGER.debug(
@@ -625,6 +644,13 @@ class CalendarEntity(Entity):
event.end_datetime_local,
)
@callback
def _async_cancel_event_listener_debouncer(self) -> None:
"""Cancel and clear the event listener debouncer."""
if self._event_listener_debouncer:
self._event_listener_debouncer.async_cancel()
self._event_listener_debouncer = None
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass.
@@ -633,6 +659,90 @@ class CalendarEntity(Entity):
for unsub in self._alarm_unsubs or ():
unsub()
self._alarm_unsubs = None
self._async_cancel_event_listener_debouncer()
@final
@callback
def async_subscribe_events(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
event_listener: Callable[[list[JsonValueType] | None], None],
) -> CALLBACK_TYPE:
"""Subscribe to calendar event updates.
Called by websocket API.
"""
if self._event_listeners is None:
self._event_listeners = []
if self._event_listener_debouncer is None:
self._event_listener_debouncer = Debouncer(
self.hass,
_LOGGER,
cooldown=EVENT_LISTENER_DEBOUNCE_COOLDOWN,
immediate=True,
function=self.async_update_event_listeners,
)
listener_data = (start_date, end_date, event_listener)
self._event_listeners.append(listener_data)
@callback
def unsubscribe() -> None:
if self._event_listeners:
self._event_listeners.remove(listener_data)
if not self._event_listeners:
self._async_cancel_event_listener_debouncer()
return unsubscribe
@final
@callback
def async_update_event_listeners(self) -> None:
"""Push updated calendar events to all listeners."""
if not self._event_listeners:
return
for start_date, end_date, listener in self._event_listeners:
self.async_update_single_event_listener(start_date, end_date, listener)
@final
@callback
def async_update_single_event_listener(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
listener: Callable[[list[JsonValueType] | None], None],
) -> None:
"""Schedule an event fetch and push to a single listener."""
self.hass.async_create_task(
self._async_update_listener(start_date, end_date, listener)
)
async def _async_update_listener(
self,
start_date: datetime.datetime,
end_date: datetime.datetime,
listener: Callable[[list[JsonValueType] | None], None],
) -> None:
"""Fetch events and push to a single listener."""
try:
events = await self.async_get_events(self.hass, start_date, end_date)
except HomeAssistantError as err:
_LOGGER.debug(
"Error fetching calendar events for %s: %s",
self.entity_id,
err,
)
listener(None)
return
event_list: list[JsonValueType] = [
dataclasses.asdict(event, dict_factory=_list_events_dict_factory)
for event in events
]
listener(event_list)
async def async_get_events(
self,
@@ -867,6 +977,65 @@ async def handle_calendar_event_update(
connection.send_result(msg["id"])
@websocket_api.websocket_command(
{
vol.Required("type"): "calendar/event/subscribe",
vol.Required("entity_id"): cv.entity_domain(DOMAIN),
vol.Required("start"): cv.datetime,
vol.Required("end"): cv.datetime,
}
)
@websocket_api.async_response
async def handle_calendar_event_subscribe(
hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any]
) -> None:
"""Subscribe to calendar event updates."""
entity_id: str = msg["entity_id"]
if not (entity := hass.data[DATA_COMPONENT].get_entity(entity_id)):
connection.send_error(
msg["id"],
ERR_NOT_FOUND,
f"Calendar entity not found: {entity_id}",
)
return
start_date = dt_util.as_local(msg["start"])
end_date = dt_util.as_local(msg["end"])
if start_date >= end_date:
connection.send_error(
msg["id"],
ERR_INVALID_FORMAT,
"Start must be before end",
)
return
subscription_id = msg["id"]
@callback
def event_listener(events: list[JsonValueType] | None) -> None:
"""Push updated calendar events to websocket."""
if subscription_id not in connection.subscriptions:
return
connection.send_message(
websocket_api.event_message(
subscription_id,
{
"events": events,
},
)
)
connection.subscriptions[subscription_id] = entity.async_subscribe_events(
start_date, end_date, event_listener
)
connection.send_result(subscription_id)
# Push initial events only to the new subscriber
entity.async_update_single_event_listener(start_date, end_date, event_listener)
def _validate_timespan(
values: dict[str, Any],
) -> tuple[datetime.datetime | datetime.date, datetime.datetime | datetime.date]:

View File

@@ -77,7 +77,12 @@ AFFECTS_TO_SELECTED_PROGRAM = "selected_program"
TRANSLATION_KEYS_PROGRAMS_MAP = {
bsh_key_to_translation_key(program.value): program
for program in ProgramKey
if program not in (ProgramKey.UNKNOWN, ProgramKey.BSH_COMMON_FAVORITE_001)
if program
not in (
ProgramKey.UNKNOWN,
ProgramKey.BSH_COMMON_FAVORITE_001,
ProgramKey.BSH_COMMON_FAVORITE_002,
)
}
PROGRAMS_TRANSLATION_KEYS_MAP = {

View File

@@ -533,7 +533,11 @@ class HomeConnectApplianceCoordinator(DataUpdateCoordinator[HomeConnectAppliance
current_program_key = program.key
program_options = program.options
if (
current_program_key == ProgramKey.BSH_COMMON_FAVORITE_001
current_program_key
in (
ProgramKey.BSH_COMMON_FAVORITE_001,
ProgramKey.BSH_COMMON_FAVORITE_002,
)
and program_options
):
# The API doesn't allow to fetch the options from the favorite program.
@@ -616,7 +620,11 @@ class HomeConnectApplianceCoordinator(DataUpdateCoordinator[HomeConnectAppliance
options_to_notify = options.copy()
options.clear()
if (
program_key == ProgramKey.BSH_COMMON_FAVORITE_001
program_key
in (
ProgramKey.BSH_COMMON_FAVORITE_001,
ProgramKey.BSH_COMMON_FAVORITE_002,
)
and (event := events.get(EventKey.BSH_COMMON_OPTION_BASE_PROGRAM))
and isinstance(event.value, str)
):

View File

@@ -23,6 +23,6 @@
"iot_class": "cloud_push",
"loggers": ["aiohomeconnect"],
"quality_scale": "platinum",
"requirements": ["aiohomeconnect==0.33.0"],
"requirements": ["aiohomeconnect==0.34.0"],
"zeroconf": ["_homeconnect._tcp.local."]
}

View File

@@ -436,7 +436,11 @@ class HomeConnectProgramSelectEntity(HomeConnectEntity, SelectEntity):
else None
)
if (
program_key == ProgramKey.BSH_COMMON_FAVORITE_001
program_key
in (
ProgramKey.BSH_COMMON_FAVORITE_001,
ProgramKey.BSH_COMMON_FAVORITE_002,
)
and (
base_program_event := self.appliance.events.get(
EventKey.BSH_COMMON_OPTION_BASE_PROGRAM

View File

@@ -498,7 +498,7 @@ class DishWasherProgramId(MieleEnum, missing_to_none=True):
intensive = 1, 26, 205
maintenance = 2, 27, 214
eco = 3, 22, 28, 200
automatic = 6, 7, 31, 32, 202
automatic = 6, 7, 31, 32, 201, 202
solar_save = 9, 34
gentle = 10, 35, 210
extra_quiet = 11, 36, 207

View File

@@ -2,13 +2,23 @@
import logging
from pyopnsense import diagnostics
from pyopnsense.exceptions import APIException
from aiopnsense import (
OPNsenseBelowMinFirmware,
OPNsenseClient,
OPNsenseConnectionError,
OPNsenseInvalidAuth,
OPNsenseInvalidURL,
OPNsensePrivilegeMissing,
OPNsenseSSLError,
OPNsenseTimeoutError,
OPNsenseUnknownFirmware,
)
import voluptuous as vol
from homeassistant.const import CONF_API_KEY, CONF_URL, CONF_VERIFY_SSL, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.typing import ConfigType
@@ -40,7 +50,7 @@ CONFIG_SCHEMA = vol.Schema(
)
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the opnsense component."""
conf = config[DOMAIN]
@@ -50,30 +60,73 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
verify_ssl = conf[CONF_VERIFY_SSL]
tracker_interfaces = conf[CONF_TRACKER_INTERFACES]
interfaces_client = diagnostics.InterfaceClient(
api_key, api_secret, url, verify_ssl, timeout=20
session = async_get_clientsession(hass, verify_ssl=verify_ssl)
client = OPNsenseClient(
url,
api_key,
api_secret,
session,
opts={"verify_ssl": verify_ssl},
)
try:
interfaces_client.get_arp()
except APIException:
_LOGGER.exception("Failure while connecting to OPNsense API endpoint")
await client.validate()
if tracker_interfaces:
interfaces_resp = await client.get_interfaces()
except OPNsenseUnknownFirmware:
_LOGGER.error("Error checking the OPNsense firmware version at %s", url)
return False
except OPNsenseBelowMinFirmware:
_LOGGER.error(
"OPNsense Firmware is below the minimum supported version at %s", url
)
return False
except OPNsenseInvalidURL:
_LOGGER.error(
"Invalid URL while connecting to OPNsense API endpoint at %s", url
)
return False
except OPNsenseTimeoutError:
_LOGGER.error("Timeout while connecting to OPNsense API endpoint at %s", url)
return False
except OPNsenseSSLError:
_LOGGER.error(
"Unable to verify SSL while connecting to OPNsense API endpoint at %s", url
)
return False
except OPNsenseInvalidAuth:
_LOGGER.error(
"Authentication failure while connecting to OPNsense API endpoint at %s",
url,
)
return False
except OPNsensePrivilegeMissing:
_LOGGER.error(
"Invalid Permissions while connecting to OPNsense API endpoint at %s",
url,
)
return False
except OPNsenseConnectionError:
_LOGGER.error(
"Connection failure while connecting to OPNsense API endpoint at %s",
url,
)
return False
if tracker_interfaces:
# Verify that specified tracker interfaces are valid
netinsight_client = diagnostics.NetworkInsightClient(
api_key, api_secret, url, verify_ssl, timeout=20
)
interfaces = list(netinsight_client.get_interfaces().values())
for interface in tracker_interfaces:
if interface not in interfaces:
known_interfaces = [
ifinfo.get("name", "") for ifinfo in interfaces_resp.values()
]
for intf_description in tracker_interfaces:
if intf_description not in known_interfaces:
_LOGGER.error(
"Specified OPNsense tracker interface %s is not found", interface
"Specified OPNsense tracker interface %s is not found",
intf_description,
)
return False
hass.data[OPNSENSE_DATA] = {
CONF_INTERFACE_CLIENT: interfaces_client,
CONF_INTERFACE_CLIENT: client,
CONF_TRACKER_INTERFACES: tracker_interfaces,
}

View File

@@ -2,7 +2,7 @@
from typing import Any, NewType
from pyopnsense import diagnostics
from aiopnsense import OPNsenseClient
from homeassistant.components.device_tracker import DeviceScanner
from homeassistant.core import HomeAssistant
@@ -27,9 +27,7 @@ async def async_get_scanner(
class OPNsenseDeviceScanner(DeviceScanner):
"""This class queries a router running OPNsense."""
def __init__(
self, client: diagnostics.InterfaceClient, interfaces: list[str]
) -> None:
def __init__(self, client: OPNsenseClient, interfaces: list[str]) -> None:
"""Initialize the scanner."""
self.last_results: dict[str, Any] = {}
self.client = client
@@ -43,9 +41,9 @@ class OPNsenseDeviceScanner(DeviceScanner):
out_devices[device["mac"]] = device
return out_devices
def scan_devices(self) -> list[str]:
async def async_scan_devices(self) -> list[str]:
"""Scan for new devices and return a list with found device IDs."""
self.update_info()
await self._async_update_info()
return list(self.last_results)
def get_device_name(self, device: str) -> str | None:
@@ -54,12 +52,12 @@ class OPNsenseDeviceScanner(DeviceScanner):
return None
return self.last_results[device].get("hostname") or None
def update_info(self) -> bool:
async def _async_update_info(self) -> bool:
"""Ensure the information from the OPNsense router is up to date.
Return boolean if scanning successful.
"""
devices = self.client.get_arp()
devices = await self.client.get_arp_table(True)
self.last_results = self._get_mac_addrs(devices)
return True

View File

@@ -1,11 +1,11 @@
{
"domain": "opnsense",
"name": "OPNsense",
"codeowners": ["@mtreinish"],
"codeowners": ["@HarlemSquirrel", "@Snuffy2"],
"documentation": "https://www.home-assistant.io/integrations/opnsense",
"integration_type": "hub",
"iot_class": "local_polling",
"loggers": ["pbr", "pyopnsense"],
"loggers": ["aiopnsense"],
"quality_scale": "legacy",
"requirements": ["pyopnsense==0.4.0"]
"requirements": ["aiopnsense==1.0.8"]
}

8
requirements_all.txt generated
View File

@@ -279,7 +279,7 @@ aioharmony==0.5.3
aiohasupervisor==0.4.3
# homeassistant.components.home_connect
aiohomeconnect==0.33.0
aiohomeconnect==0.34.0
# homeassistant.components.homekit_controller
aiohomekit==3.2.20
@@ -356,6 +356,9 @@ aiooui==0.1.9
# homeassistant.components.pegel_online
aiopegelonline==0.1.1
# homeassistant.components.opnsense
aiopnsense==1.0.8
# homeassistant.components.acmeda
aiopulse==0.4.6
@@ -2352,9 +2355,6 @@ pyopenuv==2023.02.0
# homeassistant.components.openweathermap
pyopenweathermap==0.2.2
# homeassistant.components.opnsense
pyopnsense==0.4.0
# homeassistant.components.opple
pyoppleio-legacy==1.0.8

View File

@@ -267,7 +267,7 @@ aioharmony==0.5.3
aiohasupervisor==0.4.3
# homeassistant.components.home_connect
aiohomeconnect==0.33.0
aiohomeconnect==0.34.0
# homeassistant.components.homekit_controller
aiohomekit==3.2.20
@@ -341,6 +341,9 @@ aiooui==0.1.9
# homeassistant.components.pegel_online
aiopegelonline==0.1.1
# homeassistant.components.opnsense
aiopnsense==1.0.8
# homeassistant.components.acmeda
aiopulse==0.4.6
@@ -2014,9 +2017,6 @@ pyopenuv==2023.02.0
# homeassistant.components.openweathermap
pyopenweathermap==0.2.2
# homeassistant.components.opnsense
pyopnsense==0.4.0
# homeassistant.components.osoenergy
pyosoenergyapi==1.2.4

View File

@@ -189,11 +189,6 @@ FORBIDDEN_PACKAGE_EXCEPTIONS: dict[str, dict[str, set[str]]] = {
"norway_air": {"pymetno": {"async-timeout"}},
"opengarage": {"open-garage": {"async-timeout"}},
"opensensemap": {"opensensemap-api": {"async-timeout"}},
"opnsense": {
# https://github.com/mtreinish/pyopnsense/issues/27
# pyopnsense > pbr > setuptools
"pbr": {"setuptools"}
},
"pvpc_hourly_pricing": {"aiopvpc": {"async-timeout"}},
"remote_rpi_gpio": {
# https://github.com/waveform80/colorzero/issues/9
@@ -298,10 +293,6 @@ FORBIDDEN_PACKAGE_FILES_EXCEPTIONS = {
},
# https://github.com/ejpenney/pyobihai
"obihai": {"homeassistant": {"pyobihai"}},
"opnsense": {
# Setuptools - distutils-precedence.pth
"pbr": {"setuptools"}
},
# https://github.com/iamkubi/pydactyl
"pterodactyl": {"homeassistant": {"py-dactyl"}},
"remote_rpi_gpio": {

View File

@@ -28,6 +28,7 @@ from homeassistant.util import dt as dt_util
from .conftest import MockCalendarEntity, MockConfigEntry
from tests.common import async_fire_time_changed
from tests.typing import ClientSessionGenerator, WebSocketGenerator
@@ -715,3 +716,236 @@ async def test_calendar_initial_color_precedence(
entity = TestCalendarEntity(description_color, attr_color)
assert entity.initial_color == expected_color
async def test_websocket_handle_subscribe_calendar_events(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
test_entities: list[MockCalendarEntity],
) -> None:
"""Test subscribing to calendar event updates via websocket."""
client = await hass_ws_client(hass)
start = dt_util.now()
end = start + timedelta(days=1)
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.calendar_1",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert msg["success"]
subscription_id = msg["id"]
# Should receive initial event list
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
assert "events" in msg["event"]
events = msg["event"]["events"]
assert len(events) == 1
assert events[0]["summary"] == "Future Event"
async def test_websocket_subscribe_updates_on_state_change(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
test_entities: list[MockCalendarEntity],
) -> None:
"""Test that subscribers receive updates when calendar state changes."""
client = await hass_ws_client(hass)
start = dt_util.now()
end = start + timedelta(days=1)
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.calendar_1",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert msg["success"]
subscription_id = msg["id"]
# Receive initial event list
msg = await client.receive_json()
assert msg["id"] == subscription_id
# Add a new event and trigger state update
entity = test_entities[0]
entity.create_event(
start=start + timedelta(hours=2),
end=start + timedelta(hours=3),
summary="New Event",
)
entity.async_write_ha_state()
await hass.async_block_till_done()
# Should receive updated event list
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
events = msg["event"]["events"]
assert len(events) == 2
summaries = {event["summary"] for event in events}
assert "Future Event" in summaries
assert "New Event" in summaries
async def test_websocket_subscribe_entity_not_found(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
) -> None:
"""Test subscribing to a non-existent calendar entity."""
client = await hass_ws_client(hass)
start = dt_util.now()
end = start + timedelta(days=1)
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.nonexistent",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == "not_found"
assert "Calendar entity not found" in msg["error"]["message"]
async def test_websocket_subscribe_event_fetch_error(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
test_entities: list[MockCalendarEntity],
) -> None:
"""Test subscription handles event fetch errors gracefully."""
client = await hass_ws_client(hass)
start = dt_util.now()
end = start + timedelta(days=1)
# Set up entity to fail on async_get_events
test_entities[0].async_get_events.side_effect = HomeAssistantError("API Error")
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.calendar_1",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert msg["success"]
subscription_id = msg["id"]
# Should receive None for events due to error
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
assert msg["event"]["events"] is None
async def test_websocket_subscribe_invalid_timespan(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
test_entities: list[MockCalendarEntity],
) -> None:
"""Test subscribing with start after end returns an error."""
client = await hass_ws_client(hass)
now = dt_util.now()
start = now + timedelta(days=1)
end = now
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.calendar_1",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert not msg["success"]
assert msg["error"]["code"] == "invalid_format"
assert "Start must be before end" in msg["error"]["message"]
async def test_websocket_subscribe_debounces_rapid_updates(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
test_entities: list[MockCalendarEntity],
) -> None:
"""Test that rapid state writes are debounced for event listeners."""
client = await hass_ws_client(hass)
start = dt_util.now()
end = start + timedelta(days=1)
await client.send_json_auto_id(
{
"type": "calendar/event/subscribe",
"entity_id": "calendar.calendar_1",
"start": start.isoformat(),
"end": end.isoformat(),
}
)
msg = await client.receive_json()
assert msg["success"]
subscription_id = msg["id"]
# Receive initial event list
msg = await client.receive_json()
assert msg["id"] == subscription_id
entity = test_entities[0]
entity.async_get_events.reset_mock()
# Rapidly write state multiple times
for i in range(5):
entity.create_event(
start=start + timedelta(hours=i + 2),
end=start + timedelta(hours=i + 3),
summary=f"Rapid Event {i}",
)
entity.async_write_ha_state()
await hass.async_block_till_done()
# The debouncer with immediate=True fires the first call immediately
# and coalesces the rest into one call after the cooldown.
# Without debouncing this would be 5 calls.
assert entity.async_get_events.call_count == 1
# Advance time past the debounce cooldown to fire the trailing call
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=2))
await hass.async_block_till_done()
# Should be exactly 2 total: immediate + one coalesced trailing call
assert entity.async_get_events.call_count == 2
# Drain messages: immediate update + trailing debounced update
messages: list[dict] = []
for _ in range(10):
msg = await client.receive_json()
assert msg["id"] == subscription_id
assert msg["type"] == "event"
messages.append(msg)
if len(msg["event"]["events"]) == 6: # 1 original + 5 rapid
break
else:
pytest.fail("Did not receive expected calendar event list with 6 events")
# The final message has all events
assert len(messages[-1]["event"]["events"]) == 6

View File

@@ -14,8 +14,8 @@ from homeassistant.setup import async_setup_component
@pytest.fixture(name="mocked_opnsense")
def mocked_opnsense():
"""Mock for pyopnense.diagnostics."""
with mock.patch.object(opnsense, "diagnostics") as mocked_opn:
"""Mock for aiopnsense.OPNsenseClient."""
with mock.patch.object(opnsense, "OPNsenseClient") as mocked_opn:
yield mocked_opn
@@ -23,9 +23,9 @@ async def test_get_scanner(
hass: HomeAssistant, mocked_opnsense, mock_device_tracker_conf: list[legacy.Device]
) -> None:
"""Test creating an opnsense scanner."""
interface_client = mock.MagicMock()
mocked_opnsense.InterfaceClient.return_value = interface_client
interface_client.get_arp.return_value = [
opnsense_client = mock.AsyncMock()
mocked_opnsense.return_value = opnsense_client
opnsense_client.get_arp_table.return_value = [
{
"hostname": "",
"intf": "igb1",
@@ -43,9 +43,11 @@ async def test_get_scanner(
"manufacturer": "OEM",
},
]
network_insight_client = mock.MagicMock()
mocked_opnsense.NetworkInsightClient.return_value = network_insight_client
network_insight_client.get_interfaces.return_value = {"igb0": "WAN", "igb1": "LAN"}
opnsense_client.get_interfaces.return_value = {
"wan": {"name": "WAN"},
"lan": {"name": "LAN"},
}
result = await async_setup_component(
hass,