From 5f6b4461956e5c6cb325305d245faf1de4788533 Mon Sep 17 00:00:00 2001 From: rhcp011235 Date: Wed, 18 Feb 2026 17:03:53 -0500 Subject: [PATCH 01/20] Migrate SleepIQ sensors to entity descriptions (#163213) Co-authored-by: Claude Sonnet 4.6 --- homeassistant/components/sleepiq/sensor.py | 51 ++++++++++++++----- .../sleepiq/snapshots/test_sensor.ambr | 8 +-- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/sleepiq/sensor.py b/homeassistant/components/sleepiq/sensor.py index ca4fbc186eddc..0b8f7fc50023a 100644 --- a/homeassistant/components/sleepiq/sensor.py +++ b/homeassistant/components/sleepiq/sensor.py @@ -1,10 +1,17 @@ -"""Support for SleepIQ Sensor.""" +"""Support for SleepIQ sensors.""" from __future__ import annotations +from collections.abc import Callable +from dataclasses import dataclass + from asyncsleepiq import SleepIQBed, SleepIQSleeper -from homeassistant.components.sensor import SensorEntity, SensorStateClass +from homeassistant.components.sensor import ( + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -13,7 +20,28 @@ from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator from .entity import SleepIQSleeperEntity -SENSORS = [PRESSURE, SLEEP_NUMBER] + +@dataclass(frozen=True, kw_only=True) +class SleepIQSensorEntityDescription(SensorEntityDescription): + """Describes SleepIQ sensor entity.""" + + value_fn: Callable[[SleepIQSleeper], float | int | None] + + +SENSORS: tuple[SleepIQSensorEntityDescription, ...] = ( + SleepIQSensorEntityDescription( + key=PRESSURE, + translation_key="pressure", + state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda sleeper: sleeper.pressure, + ), + SleepIQSensorEntityDescription( + key=SLEEP_NUMBER, + translation_key="sleep_number", + state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda sleeper: sleeper.sleep_number, + ), +) async def async_setup_entry( @@ -24,33 +52,32 @@ async def async_setup_entry( """Set up the SleepIQ bed sensors.""" data: SleepIQData = hass.data[DOMAIN][entry.entry_id] async_add_entities( - SleepIQSensorEntity(data.data_coordinator, bed, sleeper, sensor_type) + SleepIQSensorEntity(data.data_coordinator, bed, sleeper, description) for bed in data.client.beds.values() for sleeper in bed.sleepers - for sensor_type in SENSORS + for description in SENSORS ) class SleepIQSensorEntity( SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SensorEntity ): - """Representation of an SleepIQ Entity with CoordinatorEntity.""" + """Representation of a SleepIQ sensor.""" - _attr_icon = "mdi:bed" + entity_description: SleepIQSensorEntityDescription def __init__( self, coordinator: SleepIQDataUpdateCoordinator, bed: SleepIQBed, sleeper: SleepIQSleeper, - sensor_type: str, + description: SleepIQSensorEntityDescription, ) -> None: """Initialize the sensor.""" - self.sensor_type = sensor_type - self._attr_state_class = SensorStateClass.MEASUREMENT - super().__init__(coordinator, bed, sleeper, sensor_type) + self.entity_description = description + super().__init__(coordinator, bed, sleeper, description.key) @callback def _async_update_attrs(self) -> None: """Update sensor attributes.""" - self._attr_native_value = getattr(self.sleeper, self.sensor_type) + self._attr_native_value = self.entity_description.value_fn(self.sleeper) diff --git a/tests/components/sleepiq/snapshots/test_sensor.ambr b/tests/components/sleepiq/snapshots/test_sensor.ambr index 2bf892e2277bf..22093c0fb37f0 100644 --- a/tests/components/sleepiq/snapshots/test_sensor.ambr +++ b/tests/components/sleepiq/snapshots/test_sensor.ambr @@ -32,7 +32,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'pressure', 'unique_id': '43219_pressure', 'unit_of_measurement': None, }) @@ -85,7 +85,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'sleep_number', 'unique_id': '43219_sleep_number', 'unit_of_measurement': None, }) @@ -138,7 +138,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'pressure', 'unique_id': '98765_pressure', 'unit_of_measurement': None, }) @@ -191,7 +191,7 @@ 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': None, + 'translation_key': 'sleep_number', 'unique_id': '98765_sleep_number', 'unit_of_measurement': None, }) From 723825b579699c1059d885fc241efd4f5ea3e2bc Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Thu, 19 Feb 2026 08:06:49 +1000 Subject: [PATCH 02/20] Mark runtime-data quality as exempt in Splunk (#163359) Co-authored-by: Claude Opus 4.6 --- homeassistant/components/splunk/quality_scale.yaml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/splunk/quality_scale.yaml b/homeassistant/components/splunk/quality_scale.yaml index 0d3e5023fe037..6b736068c7ad2 100644 --- a/homeassistant/components/splunk/quality_scale.yaml +++ b/homeassistant/components/splunk/quality_scale.yaml @@ -48,9 +48,11 @@ rules: integration-owner: done reauthentication-flow: done runtime-data: - status: todo + status: exempt comment: | - Replace hass.data[DATA_FILTER] storage with entry.runtime_data. Create typed ConfigEntry in const.py as 'type SplunkConfigEntry = ConfigEntry[EntityFilter]', update async_setup_entry signature to use SplunkConfigEntry, replace hass.data[DATA_FILTER] assignments with entry.runtime_data, and update all references including line 236 in __init__.py. + Integration has no per-entry runtime state to store. The only data in + hass.data is a YAML entity filter bridged from async_setup to + async_setup_entry; no platforms or other code accesses it afterward. test-before-configure: done test-before-setup: done test-coverage: done From 122bc32f30f910d99c8cb100b7486f71566e32a5 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 18 Feb 2026 23:11:01 +0100 Subject: [PATCH 03/20] Add integration_type device to sensorpush (#163389) --- homeassistant/components/sensorpush/manifest.json | 1 + homeassistant/generated/integrations.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/sensorpush/manifest.json b/homeassistant/components/sensorpush/manifest.json index 56db6f8f2808b..8b5a093195e0c 100644 --- a/homeassistant/components/sensorpush/manifest.json +++ b/homeassistant/components/sensorpush/manifest.json @@ -16,6 +16,7 @@ "config_flow": true, "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/sensorpush", + "integration_type": "device", "iot_class": "local_push", "requirements": ["sensorpush-ble==1.9.0"] } diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 9cd7bc7853329..118aee2114c6b 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -6034,7 +6034,7 @@ "name": "SensorPush", "integrations": { "sensorpush": { - "integration_type": "hub", + "integration_type": "device", "config_flow": true, "iot_class": "local_push", "name": "SensorPush" From 2e0f7279817a5e02be65b17062048e030cec581c Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 18 Feb 2026 23:11:29 +0100 Subject: [PATCH 04/20] Add integration_type hub to senz (#163391) --- homeassistant/components/senz/manifest.json | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/senz/manifest.json b/homeassistant/components/senz/manifest.json index 96f4f7e02b1e4..aca6bce3f946e 100644 --- a/homeassistant/components/senz/manifest.json +++ b/homeassistant/components/senz/manifest.json @@ -5,6 +5,7 @@ "config_flow": true, "dependencies": ["application_credentials"], "documentation": "https://www.home-assistant.io/integrations/senz", + "integration_type": "hub", "iot_class": "cloud_polling", "loggers": ["pysenz"], "requirements": ["pysenz==1.0.2"] From be25603b76a316232f0a79f181a9522f0ef50180 Mon Sep 17 00:00:00 2001 From: mettolen <1007649+mettolen@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:11:47 +0200 Subject: [PATCH 05/20] Refactor optimistic update and delayed refresh for Liebherr integration (#163121) --- homeassistant/components/liebherr/const.py | 3 ++ homeassistant/components/liebherr/entity.py | 32 ++++++++++++++-- homeassistant/components/liebherr/number.py | 37 ++++++------------- .../components/liebherr/strings.json | 2 +- homeassistant/components/liebherr/switch.py | 35 +----------------- tests/components/liebherr/conftest.py | 11 ++++++ tests/components/liebherr/test_number.py | 7 +++- tests/components/liebherr/test_switch.py | 2 +- 8 files changed, 65 insertions(+), 64 deletions(-) diff --git a/homeassistant/components/liebherr/const.py b/homeassistant/components/liebherr/const.py index f02c28e46d199..82af6817c0966 100644 --- a/homeassistant/components/liebherr/const.py +++ b/homeassistant/components/liebherr/const.py @@ -1,6 +1,9 @@ """Constants for the liebherr integration.""" +from datetime import timedelta from typing import Final DOMAIN: Final = "liebherr" MANUFACTURER: Final = "Liebherr" + +REFRESH_DELAY: Final = timedelta(seconds=5) diff --git a/homeassistant/components/liebherr/entity.py b/homeassistant/components/liebherr/entity.py index 1e5dc7ca38572..eb343491dce98 100644 --- a/homeassistant/components/liebherr/entity.py +++ b/homeassistant/components/liebherr/entity.py @@ -2,12 +2,22 @@ from __future__ import annotations -from pyliebherrhomeapi import TemperatureControl, ZonePosition - +import asyncio +from collections.abc import Coroutine +from typing import Any + +from pyliebherrhomeapi import ( + LiebherrConnectionError, + LiebherrTimeoutError, + TemperatureControl, + ZonePosition, +) + +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import DOMAIN, MANUFACTURER +from .const import DOMAIN, MANUFACTURER, REFRESH_DELAY from .coordinator import LiebherrCoordinator # Zone position to translation key mapping @@ -44,6 +54,22 @@ def __init__( model_id=device.device_name, ) + async def _async_send_command( + self, + command: Coroutine[Any, Any, None], + ) -> None: + """Send a command with error handling and delayed refresh.""" + try: + await command + except (LiebherrConnectionError, LiebherrTimeoutError) as err: + raise HomeAssistantError( + translation_domain=DOMAIN, + translation_key="communication_error", + ) from err + + await asyncio.sleep(REFRESH_DELAY.total_seconds()) + await self.coordinator.async_request_refresh() + class LiebherrZoneEntity(LiebherrEntity): """Base entity for zone-based Liebherr entities. diff --git a/homeassistant/components/liebherr/number.py b/homeassistant/components/liebherr/number.py index 0841d29174a27..6ba938e0a2cad 100644 --- a/homeassistant/components/liebherr/number.py +++ b/homeassistant/components/liebherr/number.py @@ -4,13 +4,9 @@ from collections.abc import Callable from dataclasses import dataclass +from typing import TYPE_CHECKING -from pyliebherrhomeapi import ( - LiebherrConnectionError, - LiebherrTimeoutError, - TemperatureControl, - TemperatureUnit, -) +from pyliebherrhomeapi import TemperatureControl, TemperatureUnit from homeassistant.components.number import ( DEFAULT_MAX_VALUE, @@ -21,10 +17,8 @@ ) from homeassistant.const import UnitOfTemperature from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN from .coordinator import LiebherrConfigEntry, LiebherrCoordinator from .entity import LiebherrZoneEntity @@ -109,10 +103,9 @@ def native_unit_of_measurement(self) -> str | None: @property def native_value(self) -> float | None: """Return the current value.""" - # temperature_control is guaranteed to exist when entity is available - return self.entity_description.value_fn( - self.temperature_control # type: ignore[arg-type] - ) + if TYPE_CHECKING: + assert self.temperature_control is not None + return self.entity_description.value_fn(self.temperature_control) @property def native_min_value(self) -> float: @@ -139,27 +132,21 @@ def available(self) -> bool: async def async_set_native_value(self, value: float) -> None: """Set new value.""" - # temperature_control is guaranteed to exist when entity is available + if TYPE_CHECKING: + assert self.temperature_control is not None temp_control = self.temperature_control unit = ( TemperatureUnit.FAHRENHEIT - if temp_control.unit == TemperatureUnit.FAHRENHEIT # type: ignore[union-attr] + if temp_control.unit == TemperatureUnit.FAHRENHEIT else TemperatureUnit.CELSIUS ) - try: - await self.coordinator.client.set_temperature( + await self._async_send_command( + self.coordinator.client.set_temperature( device_id=self.coordinator.device_id, zone_id=self._zone_id, target=int(value), unit=unit, - ) - except (LiebherrConnectionError, LiebherrTimeoutError) as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="communication_error", - translation_placeholders={"error": str(err)}, - ) from err - - await self.coordinator.async_request_refresh() + ), + ) diff --git a/homeassistant/components/liebherr/strings.json b/homeassistant/components/liebherr/strings.json index 3549760f577f0..dd4af5c6d5aa0 100644 --- a/homeassistant/components/liebherr/strings.json +++ b/homeassistant/components/liebherr/strings.json @@ -93,7 +93,7 @@ }, "exceptions": { "communication_error": { - "message": "An error occurred while communicating with the device: {error}" + "message": "An error occurred while communicating with the device" } } } diff --git a/homeassistant/components/liebherr/switch.py b/homeassistant/components/liebherr/switch.py index db07860d677fb..c956fa163c1dc 100644 --- a/homeassistant/components/liebherr/switch.py +++ b/homeassistant/components/liebherr/switch.py @@ -2,29 +2,20 @@ from __future__ import annotations -import asyncio from collections.abc import Awaitable, Callable from dataclasses import dataclass from typing import TYPE_CHECKING, Any -from pyliebherrhomeapi import ( - LiebherrConnectionError, - LiebherrTimeoutError, - ToggleControl, - ZonePosition, -) +from pyliebherrhomeapi import ToggleControl, ZonePosition from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN from .coordinator import LiebherrConfigEntry, LiebherrCoordinator from .entity import ZONE_POSITION_MAP, LiebherrEntity PARALLEL_UPDATES = 1 -REFRESH_DELAY = 5 # Control names from the API CONTROL_SUPERCOOL = "supercool" @@ -144,7 +135,6 @@ class LiebherrDeviceSwitch(LiebherrEntity, SwitchEntity): entity_description: LiebherrSwitchEntityDescription _zone_id: int | None = None - _optimistic_state: bool | None = None def __init__( self, @@ -171,17 +161,10 @@ def _toggle_control(self) -> ToggleControl | None: @property def is_on(self) -> bool | None: """Return true if the switch is on.""" - if self._optimistic_state is not None: - return self._optimistic_state if TYPE_CHECKING: assert self._toggle_control is not None return self._toggle_control.value - def _handle_coordinator_update(self) -> None: - """Handle updated data from the coordinator.""" - self._optimistic_state = None - super()._handle_coordinator_update() - @property def available(self) -> bool: """Return if entity is available.""" @@ -205,21 +188,7 @@ async def _async_call_set_fn(self, value: bool) -> None: async def _async_set_value(self, value: bool) -> None: """Set the switch value.""" - try: - await self._async_call_set_fn(value) - except (LiebherrConnectionError, LiebherrTimeoutError) as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="communication_error", - translation_placeholders={"error": str(err)}, - ) from err - - # Track expected state locally to avoid mutating shared coordinator data - self._optimistic_state = value - self.async_write_ha_state() - - await asyncio.sleep(REFRESH_DELAY) - await self.coordinator.async_request_refresh() + await self._async_send_command(self._async_call_set_fn(value)) class LiebherrZoneSwitch(LiebherrDeviceSwitch): diff --git a/tests/components/liebherr/conftest.py b/tests/components/liebherr/conftest.py index 536b76a34b127..f3a253ea022ea 100644 --- a/tests/components/liebherr/conftest.py +++ b/tests/components/liebherr/conftest.py @@ -2,6 +2,7 @@ from collections.abc import Generator import copy +from datetime import timedelta from unittest.mock import AsyncMock, MagicMock, patch from pyliebherrhomeapi import ( @@ -86,6 +87,16 @@ ) +@pytest.fixture(autouse=True) +def patch_refresh_delay() -> Generator[None]: + """Patch REFRESH_DELAY to 0 to avoid delays in tests.""" + with patch( + "homeassistant.components.liebherr.entity.REFRESH_DELAY", + timedelta(seconds=0), + ): + yield + + @pytest.fixture def mock_setup_entry() -> Generator[AsyncMock]: """Override async_setup_entry.""" diff --git a/tests/components/liebherr/test_number.py b/tests/components/liebherr/test_number.py index 480df1413e070..95ccdc6bfa865 100644 --- a/tests/components/liebherr/test_number.py +++ b/tests/components/liebherr/test_number.py @@ -172,6 +172,8 @@ async def test_set_temperature( """Test setting the temperature.""" entity_id = "number.test_fridge_top_zone_setpoint" + initial_call_count = mock_liebherr_client.get_device_state.call_count + await hass.services.async_call( NUMBER_DOMAIN, SERVICE_SET_VALUE, @@ -186,6 +188,9 @@ async def test_set_temperature( unit=TemperatureUnit.CELSIUS, ) + # Verify coordinator refresh was triggered + assert mock_liebherr_client.get_device_state.call_count > initial_call_count + @pytest.mark.usefixtures("init_integration") async def test_set_temperature_failure( @@ -201,7 +206,7 @@ async def test_set_temperature_failure( with pytest.raises( HomeAssistantError, - match="An error occurred while communicating with the device: Connection failed", + match="An error occurred while communicating with the device", ): await hass.services.async_call( NUMBER_DOMAIN, diff --git a/tests/components/liebherr/test_switch.py b/tests/components/liebherr/test_switch.py index 9bed382f48fa5..3fcfd79cd0929 100644 --- a/tests/components/liebherr/test_switch.py +++ b/tests/components/liebherr/test_switch.py @@ -140,7 +140,7 @@ async def test_switch_failure( with pytest.raises( HomeAssistantError, - match="An error occurred while communicating with the device: Connection failed", + match="An error occurred while communicating with the device", ): await hass.services.async_call( SWITCH_DOMAIN, From ba547c6bdb7020a127ee65c73aaf7b9d2f0aaec4 Mon Sep 17 00:00:00 2001 From: Artur Pragacz <49985303+arturpragacz@users.noreply.github.com> Date: Wed, 18 Feb 2026 23:26:57 +0100 Subject: [PATCH 06/20] Add channel muting switches to Onkyo (#162605) Co-authored-by: Joost Lekkerkerker --- homeassistant/components/onkyo/__init__.py | 5 +- homeassistant/components/onkyo/coordinator.py | 167 +++++ .../components/onkyo/media_player.py | 2 +- homeassistant/components/onkyo/switch.py | 96 +++ .../onkyo/snapshots/test_switch.ambr | 638 ++++++++++++++++++ tests/components/onkyo/test_switch.py | 220 ++++++ 6 files changed, 1126 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/onkyo/coordinator.py create mode 100644 homeassistant/components/onkyo/switch.py create mode 100644 tests/components/onkyo/snapshots/test_switch.ambr create mode 100644 tests/components/onkyo/test_switch.py diff --git a/homeassistant/components/onkyo/__init__.py b/homeassistant/components/onkyo/__init__.py index df09189646d8c..ed2bb2904cd0c 100644 --- a/homeassistant/components/onkyo/__init__.py +++ b/homeassistant/components/onkyo/__init__.py @@ -17,12 +17,13 @@ InputSource, ListeningMode, ) +from .coordinator import ChannelMutingCoordinator from .receiver import ReceiverManager, async_interview from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -PLATFORMS = [Platform.MEDIA_PLAYER] +PLATFORMS = [Platform.MEDIA_PLAYER, Platform.SWITCH] CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) @@ -66,6 +67,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: OnkyoConfigEntry) -> boo entry.runtime_data = OnkyoData(manager, sources, sound_modes) + ChannelMutingCoordinator(hass, entry, manager) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) if error := await manager.start(): diff --git a/homeassistant/components/onkyo/coordinator.py b/homeassistant/components/onkyo/coordinator.py new file mode 100644 index 0000000000000..d418b09ad04b8 --- /dev/null +++ b/homeassistant/components/onkyo/coordinator.py @@ -0,0 +1,167 @@ +"""Onkyo coordinators.""" + +from __future__ import annotations + +import asyncio +from enum import StrEnum +import logging +from typing import TYPE_CHECKING, cast + +from aioonkyo import Kind, Status, Zone, command, query, status + +from homeassistant.core import HomeAssistant +from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator + +from .const import DOMAIN +from .receiver import ReceiverManager + +if TYPE_CHECKING: + from . import OnkyoConfigEntry + +_LOGGER = logging.getLogger(__name__) + + +POWER_ON_QUERY_DELAY = 4 + + +class Channel(StrEnum): + """Audio channel.""" + + FRONT_LEFT = "front_left" + FRONT_RIGHT = "front_right" + CENTER = "center" + SURROUND_LEFT = "surround_left" + SURROUND_RIGHT = "surround_right" + SURROUND_BACK_LEFT = "surround_back_left" + SURROUND_BACK_RIGHT = "surround_back_right" + SUBWOOFER = "subwoofer" + HEIGHT_1_LEFT = "height_1_left" + HEIGHT_1_RIGHT = "height_1_right" + HEIGHT_2_LEFT = "height_2_left" + HEIGHT_2_RIGHT = "height_2_right" + SUBWOOFER_2 = "subwoofer_2" + + +ChannelMutingData = dict[Channel, status.ChannelMuting.Param] +ChannelMutingDesired = dict[Channel, command.ChannelMuting.Param] + + +class ChannelMutingCoordinator(DataUpdateCoordinator[ChannelMutingData]): + """Coordinator for channel muting state.""" + + config_entry: OnkyoConfigEntry + + def __init__( + self, + hass: HomeAssistant, + config_entry: OnkyoConfigEntry, + manager: ReceiverManager, + ) -> None: + """Initialize the coordinator.""" + super().__init__( + hass, + _LOGGER, + config_entry=config_entry, + name="onkyo_channel_muting", + update_interval=None, + ) + + self.manager = manager + + self.data = ChannelMutingData() + self._desired = ChannelMutingDesired() + + self._entities_added = False + + self._query_state_task: asyncio.Task[None] | None = None + + manager.callbacks.connect.append(self._connect_callback) + manager.callbacks.disconnect.append(self._disconnect_callback) + manager.callbacks.update.append(self._update_callback) + + config_entry.async_on_unload(self._cancel_tasks) + + async def _connect_callback(self, _reconnect: bool) -> None: + """Receiver (re)connected.""" + await self.manager.write(query.ChannelMuting()) + + async def _disconnect_callback(self) -> None: + """Receiver disconnected.""" + self._cancel_tasks() + self.async_set_updated_data(self.data) + + def _cancel_tasks(self) -> None: + """Cancel the tasks.""" + if self._query_state_task is not None: + self._query_state_task.cancel() + self._query_state_task = None + + def _query_state(self, delay: float = 0) -> None: + """Query the receiver for all the info, that we care about.""" + if self._query_state_task is not None: + self._query_state_task.cancel() + self._query_state_task = None + + async def coro() -> None: + if delay: + await asyncio.sleep(delay) + await self.manager.write(query.ChannelMuting()) + self._query_state_task = None + + self._query_state_task = asyncio.create_task(coro()) + + async def _async_update_data(self) -> ChannelMutingData: + """Respond to a data update request.""" + self._query_state() + return self.data + + async def async_send_command( + self, channel: Channel, param: command.ChannelMuting.Param + ) -> None: + """Send muting command for a channel.""" + self._desired[channel] = param + message_data: ChannelMutingDesired = self.data | self._desired + message = command.ChannelMuting(**message_data) # type: ignore[misc] + await self.manager.write(message) + + async def _update_callback(self, message: Status) -> None: + """New message from the receiver.""" + match message: + case status.NotAvailable(kind=Kind.CHANNEL_MUTING): + not_available = True + case status.ChannelMuting(): + not_available = False + case status.Power(zone=Zone.MAIN, param=status.Power.Param.ON): + self._query_state(POWER_ON_QUERY_DELAY) + return + case _: + return + + if not self._entities_added: + _LOGGER.debug( + "Discovered %s on %s (%s)", + self.name, + self.manager.info.model_name, + self.manager.info.host, + ) + self._entities_added = True + async_dispatcher_send( + self.hass, + f"{DOMAIN}_{self.config_entry.entry_id}_channel_muting", + self, + ) + + if not_available: + self.data.clear() + self._desired.clear() + self.async_set_updated_data(self.data) + else: + message = cast(status.ChannelMuting, message) + self.data = {channel: getattr(message, channel) for channel in Channel} + self._desired = { + channel: desired + for channel, desired in self._desired.items() + if self.data[channel] != desired + } + self.async_set_updated_data(self.data) diff --git a/homeassistant/components/onkyo/media_player.py b/homeassistant/components/onkyo/media_player.py index 37065fd5aecfc..e69c9ef05434c 100644 --- a/homeassistant/components/onkyo/media_player.py +++ b/homeassistant/components/onkyo/media_player.py @@ -100,7 +100,7 @@ async def async_setup_entry( entry: OnkyoConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: - """Set up MediaPlayer for config entry.""" + """Set up media player platform for config entry.""" data = entry.runtime_data manager = data.manager diff --git a/homeassistant/components/onkyo/switch.py b/homeassistant/components/onkyo/switch.py new file mode 100644 index 0000000000000..f60c1c1ddcb56 --- /dev/null +++ b/homeassistant/components/onkyo/switch.py @@ -0,0 +1,96 @@ +"""Switch platform.""" + +from __future__ import annotations + +import logging +from typing import TYPE_CHECKING, Any + +from aioonkyo import command, status + +from homeassistant.components.switch import SwitchEntity +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import Channel, ChannelMutingCoordinator + +if TYPE_CHECKING: + from . import OnkyoConfigEntry + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: OnkyoConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up switch platform for config entry.""" + + @callback + def async_add_channel_muting_entities( + coordinator: ChannelMutingCoordinator, + ) -> None: + """Add channel muting switch entities.""" + async_add_entities( + OnkyoChannelMutingSwitch(coordinator, channel) for channel in Channel + ) + + entry.async_on_unload( + async_dispatcher_connect( + hass, + f"{DOMAIN}_{entry.entry_id}_channel_muting", + async_add_channel_muting_entities, + ) + ) + + +class OnkyoChannelMutingSwitch( + CoordinatorEntity[ChannelMutingCoordinator], SwitchEntity +): + """Onkyo Receiver Channel Muting Switch (one per channel).""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: ChannelMutingCoordinator, + channel: Channel, + ) -> None: + """Initialize the switch entity.""" + super().__init__(coordinator) + + self._channel = channel + + name = coordinator.manager.info.model_name + channel_name = channel.replace("_", " ") + identifier = coordinator.manager.info.identifier + self._attr_name = f"{name} Mute {channel_name}" + self._attr_unique_id = f"{identifier}-channel_muting-{channel}" + + @property + def available(self) -> bool: + """Return if entity is available.""" + return self.coordinator.manager.connected + + async def async_turn_on(self, **kwargs: Any) -> None: + """Mute the channel.""" + await self.coordinator.async_send_command( + self._channel, command.ChannelMuting.Param.ON + ) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Unmute the channel.""" + await self.coordinator.async_send_command( + self._channel, command.ChannelMuting.Param.OFF + ) + + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + value = self.coordinator.data.get(self._channel) + self._attr_is_on = ( + None if value is None else value == status.ChannelMuting.Param.ON + ) + super()._handle_coordinator_update() diff --git a/tests/components/onkyo/snapshots/test_switch.ambr b/tests/components/onkyo/snapshots/test_switch.ambr new file mode 100644 index 0000000000000..122067e0106e8 --- /dev/null +++ b/tests/components/onkyo/snapshots/test_switch.ambr @@ -0,0 +1,638 @@ +# serializer version: 1 +# name: test_entities[switch.tx_nr7100_mute_center-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_center', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute center', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute center', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-center', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_center-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute center', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_center', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_front_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_front_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute front left', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute front left', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-front_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_front_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute front left', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_front_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_front_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_front_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute front right', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute front right', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-front_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_front_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute front right', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_front_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_1_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_height_1_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute height 1 left', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute height 1 left', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-height_1_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_1_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute height 1 left', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_height_1_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_1_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_height_1_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute height 1 right', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute height 1 right', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-height_1_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_1_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute height 1 right', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_height_1_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_2_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_height_2_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute height 2 left', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute height 2 left', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-height_2_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_2_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute height 2 left', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_height_2_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_2_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_height_2_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute height 2 right', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute height 2 right', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-height_2_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_height_2_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute height 2 right', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_height_2_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_subwoofer-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_subwoofer', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute subwoofer', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute subwoofer', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-subwoofer', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_subwoofer-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute subwoofer', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_subwoofer', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_subwoofer_2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_subwoofer_2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute subwoofer 2', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute subwoofer 2', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-subwoofer_2', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_subwoofer_2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute subwoofer 2', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_subwoofer_2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_back_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_surround_back_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute surround back left', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute surround back left', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-surround_back_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_back_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute surround back left', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_surround_back_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_back_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_surround_back_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute surround back right', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute surround back right', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-surround_back_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_back_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute surround back right', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_surround_back_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_left-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_surround_left', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute surround left', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute surround left', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-surround_left', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_left-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute surround left', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_surround_left', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_right-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': None, + 'entity_id': 'switch.tx_nr7100_mute_surround_right', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'TX-NR7100 Mute surround right', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'TX-NR7100 Mute surround right', + 'platform': 'onkyo', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '0009B0123456-channel_muting-surround_right', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[switch.tx_nr7100_mute_surround_right-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'TX-NR7100 Mute surround right', + }), + 'context': , + 'entity_id': 'switch.tx_nr7100_mute_surround_right', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/onkyo/test_switch.py b/tests/components/onkyo/test_switch.py new file mode 100644 index 0000000000000..00ffdfb87d49a --- /dev/null +++ b/tests/components/onkyo/test_switch.py @@ -0,0 +1,220 @@ +"""Test Onkyo switch platform.""" + +import asyncio +from collections.abc import AsyncGenerator +from unittest.mock import AsyncMock, patch + +from aioonkyo import Code, Instruction, Kind, Zone, command, query, status +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.homeassistant import ( + DOMAIN as HOMEASSISTANT_DOMAIN, + SERVICE_UPDATE_ENTITY, +) +from homeassistant.components.onkyo.coordinator import Channel +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN +from homeassistant.const import ( + ATTR_ENTITY_ID, + SERVICE_TURN_OFF, + SERVICE_TURN_ON, + STATE_OFF, + STATE_ON, + STATE_UNAVAILABLE, + STATE_UNKNOWN, + Platform, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er +from homeassistant.setup import async_setup_component + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + +ENTITY_ID = "switch.tx_nr7100_mute_front_left" + + +def _channel_muting_status( + **overrides: status.ChannelMuting.Param, +) -> status.ChannelMuting: + """Create a ChannelMuting status with all channels OFF, with overrides.""" + params = dict.fromkeys(Channel, status.ChannelMuting.Param.OFF) + params.update(overrides) + return status.ChannelMuting( + Code.from_kind_zone(Kind.CHANNEL_MUTING, Zone.MAIN), + None, + **params, + ) + + +@pytest.fixture(autouse=True) +async def auto_setup_integration( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_receiver: AsyncMock, + read_queue: asyncio.Queue, + writes: list[Instruction], +) -> AsyncGenerator[None]: + """Auto setup integration.""" + read_queue.put_nowait( + _channel_muting_status( + front_right=status.ChannelMuting.Param.ON, + center=status.ChannelMuting.Param.ON, + ) + ) + + with ( + patch( + "homeassistant.components.onkyo.coordinator.POWER_ON_QUERY_DELAY", + 0, + ), + patch("homeassistant.components.onkyo.PLATFORMS", [Platform.SWITCH]), + ): + await setup_integration(hass, mock_config_entry) + writes.clear() + yield + + +async def test_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test entities.""" + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_state_changes(hass: HomeAssistant, read_queue: asyncio.Queue) -> None: + """Test NotAvailable message clears channel muting state.""" + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state == STATE_OFF + + read_queue.put_nowait( + _channel_muting_status(front_left=status.ChannelMuting.Param.ON) + ) + await asyncio.sleep(0) + + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state == STATE_ON + + read_queue.put_nowait( + status.NotAvailable( + Code.from_kind_zone(Kind.CHANNEL_MUTING, Zone.MAIN), + None, + Kind.CHANNEL_MUTING, + ) + ) + await asyncio.sleep(0) + + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state == STATE_UNKNOWN + + +async def test_availability(hass: HomeAssistant, read_queue: asyncio.Queue) -> None: + """Test entity availability on disconnect and reconnect.""" + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state != STATE_UNAVAILABLE + + # Simulate a disconnect + read_queue.put_nowait(None) + await asyncio.sleep(0) + + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state == STATE_UNAVAILABLE + + # Simulate first status update after reconnect + read_queue.put_nowait( + _channel_muting_status(front_left=status.ChannelMuting.Param.ON) + ) + await asyncio.sleep(0) + + assert (state := hass.states.get(ENTITY_ID)) is not None + assert state.state != STATE_UNAVAILABLE + + +@pytest.mark.parametrize( + ("action", "message"), + [ + ( + SERVICE_TURN_ON, + command.ChannelMuting( + front_left=command.ChannelMuting.Param.ON, + front_right=command.ChannelMuting.Param.ON, + center=command.ChannelMuting.Param.ON, + ), + ), + ( + SERVICE_TURN_OFF, + command.ChannelMuting( + front_right=command.ChannelMuting.Param.ON, + center=command.ChannelMuting.Param.ON, + ), + ), + ], +) +async def test_actions( + hass: HomeAssistant, + writes: list[Instruction], + action: str, + message: Instruction, +) -> None: + """Test actions.""" + await hass.services.async_call( + SWITCH_DOMAIN, + action, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + assert writes[0] == message + + +async def test_query_state_task( + read_queue: asyncio.Queue, writes: list[Instruction] +) -> None: + """Test query state task.""" + read_queue.put_nowait( + status.Power( + Code.from_kind_zone(Kind.POWER, Zone.MAIN), None, status.Power.Param.STANDBY + ) + ) + read_queue.put_nowait( + status.Power( + Code.from_kind_zone(Kind.POWER, Zone.MAIN), None, status.Power.Param.ON + ) + ) + read_queue.put_nowait( + status.Power( + Code.from_kind_zone(Kind.POWER, Zone.MAIN), None, status.Power.Param.STANDBY + ) + ) + read_queue.put_nowait( + status.Power( + Code.from_kind_zone(Kind.POWER, Zone.MAIN), None, status.Power.Param.ON + ) + ) + + await asyncio.sleep(0.1) + + queries = [w for w in writes if isinstance(w, query.ChannelMuting)] + assert len(queries) == 1 + + +async def test_update_entity( + hass: HomeAssistant, + writes: list[Instruction], +) -> None: + """Test manual entity update.""" + await async_setup_component(hass, HOMEASSISTANT_DOMAIN, {}) + + await hass.services.async_call( + HOMEASSISTANT_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: ENTITY_ID}, + blocking=True, + ) + await asyncio.sleep(0) + + queries = [w for w in writes if isinstance(w, query.ChannelMuting)] + assert len(queries) == 1 From 6be1e4065fb71bf71bb4ae93606a432712f02f09 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Wed, 18 Feb 2026 23:27:47 +0100 Subject: [PATCH 07/20] Add Powerfox Local integration (#163302) --- .strict-typing | 1 + CODEOWNERS | 2 + homeassistant/brands/powerfox.json | 5 + .../components/powerfox_local/__init__.py | 30 ++ .../components/powerfox_local/config_flow.py | 107 +++++++ .../components/powerfox_local/const.py | 11 + .../components/powerfox_local/coordinator.py | 48 +++ .../components/powerfox_local/entity.py | 28 ++ .../components/powerfox_local/manifest.json | 17 ++ .../powerfox_local/quality_scale.yaml | 90 ++++++ .../components/powerfox_local/sensor.py | 112 +++++++ .../components/powerfox_local/strings.json | 50 +++ homeassistant/generated/config_flows.py | 1 + homeassistant/generated/integrations.json | 17 +- homeassistant/generated/zeroconf.py | 4 + mypy.ini | 10 + requirements_all.txt | 1 + requirements_test_all.txt | 1 + tests/components/powerfox_local/__init__.py | 17 ++ tests/components/powerfox_local/conftest.py | 68 +++++ .../powerfox_local/snapshots/test_sensor.ambr | 286 ++++++++++++++++++ .../powerfox_local/test_config_flow.py | 186 ++++++++++++ tests/components/powerfox_local/test_init.py | 45 +++ .../components/powerfox_local/test_sensor.py | 56 ++++ 24 files changed, 1190 insertions(+), 3 deletions(-) create mode 100644 homeassistant/brands/powerfox.json create mode 100644 homeassistant/components/powerfox_local/__init__.py create mode 100644 homeassistant/components/powerfox_local/config_flow.py create mode 100644 homeassistant/components/powerfox_local/const.py create mode 100644 homeassistant/components/powerfox_local/coordinator.py create mode 100644 homeassistant/components/powerfox_local/entity.py create mode 100644 homeassistant/components/powerfox_local/manifest.json create mode 100644 homeassistant/components/powerfox_local/quality_scale.yaml create mode 100644 homeassistant/components/powerfox_local/sensor.py create mode 100644 homeassistant/components/powerfox_local/strings.json create mode 100644 tests/components/powerfox_local/__init__.py create mode 100644 tests/components/powerfox_local/conftest.py create mode 100644 tests/components/powerfox_local/snapshots/test_sensor.ambr create mode 100644 tests/components/powerfox_local/test_config_flow.py create mode 100644 tests/components/powerfox_local/test_init.py create mode 100644 tests/components/powerfox_local/test_sensor.py diff --git a/.strict-typing b/.strict-typing index 90c915e03272c..d7df44c9d64ca 100644 --- a/.strict-typing +++ b/.strict-typing @@ -419,6 +419,7 @@ homeassistant.components.plugwise.* homeassistant.components.pooldose.* homeassistant.components.portainer.* homeassistant.components.powerfox.* +homeassistant.components.powerfox_local.* homeassistant.components.powerwall.* homeassistant.components.private_ble_device.* homeassistant.components.prometheus.* diff --git a/CODEOWNERS b/CODEOWNERS index 90bd4f6e4d70f..17da207490389 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1283,6 +1283,8 @@ build.json @home-assistant/supervisor /tests/components/portainer/ @erwindouna /homeassistant/components/powerfox/ @klaasnicolaas /tests/components/powerfox/ @klaasnicolaas +/homeassistant/components/powerfox_local/ @klaasnicolaas +/tests/components/powerfox_local/ @klaasnicolaas /homeassistant/components/powerwall/ @bdraco @jrester @daniel-simpson /tests/components/powerwall/ @bdraco @jrester @daniel-simpson /homeassistant/components/prana/ @prana-dev-official diff --git a/homeassistant/brands/powerfox.json b/homeassistant/brands/powerfox.json new file mode 100644 index 0000000000000..7b3601f7db47e --- /dev/null +++ b/homeassistant/brands/powerfox.json @@ -0,0 +1,5 @@ +{ + "domain": "powerfox", + "name": "Powerfox", + "integrations": ["powerfox", "powerfox_local"] +} diff --git a/homeassistant/components/powerfox_local/__init__.py b/homeassistant/components/powerfox_local/__init__.py new file mode 100644 index 0000000000000..89398607fa710 --- /dev/null +++ b/homeassistant/components/powerfox_local/__init__.py @@ -0,0 +1,30 @@ +"""The Powerfox Local integration.""" + +from __future__ import annotations + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant + +from .coordinator import PowerfoxLocalConfigEntry, PowerfoxLocalDataUpdateCoordinator + +PLATFORMS: list[Platform] = [Platform.SENSOR] + + +async def async_setup_entry( + hass: HomeAssistant, entry: PowerfoxLocalConfigEntry +) -> bool: + """Set up Powerfox Local from a config entry.""" + coordinator = PowerfoxLocalDataUpdateCoordinator(hass, entry) + await coordinator.async_config_entry_first_refresh() + + entry.runtime_data = coordinator + + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + return True + + +async def async_unload_entry( + hass: HomeAssistant, entry: PowerfoxLocalConfigEntry +) -> bool: + """Unload a config entry.""" + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/powerfox_local/config_flow.py b/homeassistant/components/powerfox_local/config_flow.py new file mode 100644 index 0000000000000..94e67a6912139 --- /dev/null +++ b/homeassistant/components/powerfox_local/config_flow.py @@ -0,0 +1,107 @@ +"""Config flow for Powerfox Local integration.""" + +from __future__ import annotations + +from typing import Any + +from powerfox import PowerfoxAuthenticationError, PowerfoxConnectionError, PowerfoxLocal +import voluptuous as vol + +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult +from homeassistant.const import CONF_API_KEY, CONF_HOST +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo + +from .const import DOMAIN + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_HOST): str, + vol.Required(CONF_API_KEY): str, + } +) + + +class PowerfoxLocalConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for Powerfox Local.""" + + _host: str + _api_key: str + _device_id: str + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the user step.""" + errors: dict[str, str] = {} + + if user_input is not None: + self._host = user_input[CONF_HOST] + self._api_key = user_input[CONF_API_KEY] + self._device_id = self._api_key + + try: + await self._async_validate_connection() + except PowerfoxAuthenticationError: + errors["base"] = "invalid_auth" + except PowerfoxConnectionError: + errors["base"] = "cannot_connect" + else: + return self._async_create_entry() + + return self.async_show_form( + step_id="user", + data_schema=STEP_USER_DATA_SCHEMA, + errors=errors, + ) + + async def async_step_zeroconf( + self, discovery_info: ZeroconfServiceInfo + ) -> ConfigFlowResult: + """Handle zeroconf discovery.""" + self._host = discovery_info.host + self._device_id = discovery_info.properties["id"] + self._api_key = self._device_id + + try: + await self._async_validate_connection() + except PowerfoxAuthenticationError, PowerfoxConnectionError: + return self.async_abort(reason="cannot_connect") + + self.context["title_placeholders"] = { + "name": f"Poweropti ({self._device_id[-5:]})" + } + + self._set_confirm_only() + return self.async_show_form( + step_id="zeroconf_confirm", + description_placeholders={"host": self._host}, + ) + + async def async_step_zeroconf_confirm( + self, _: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a confirmation flow for zeroconf discovery.""" + return self._async_create_entry() + + def _async_create_entry(self) -> ConfigFlowResult: + """Create a config entry.""" + return self.async_create_entry( + title=f"Poweropti ({self._device_id[-5:]})", + data={ + CONF_HOST: self._host, + CONF_API_KEY: self._api_key, + }, + ) + + async def _async_validate_connection(self) -> None: + """Validate the connection and set unique ID.""" + client = PowerfoxLocal( + host=self._host, + api_key=self._api_key, + session=async_get_clientsession(self.hass), + ) + await client.value() + + await self.async_set_unique_id(self._device_id) + self._abort_if_unique_id_configured(updates={CONF_HOST: self._host}) diff --git a/homeassistant/components/powerfox_local/const.py b/homeassistant/components/powerfox_local/const.py new file mode 100644 index 0000000000000..f600db578aea7 --- /dev/null +++ b/homeassistant/components/powerfox_local/const.py @@ -0,0 +1,11 @@ +"""Constants for the Powerfox Local integration.""" + +from __future__ import annotations + +from datetime import timedelta +import logging +from typing import Final + +DOMAIN: Final = "powerfox_local" +LOGGER = logging.getLogger(__package__) +SCAN_INTERVAL = timedelta(seconds=5) diff --git a/homeassistant/components/powerfox_local/coordinator.py b/homeassistant/components/powerfox_local/coordinator.py new file mode 100644 index 0000000000000..62c7481c4187f --- /dev/null +++ b/homeassistant/components/powerfox_local/coordinator.py @@ -0,0 +1,48 @@ +"""Coordinator for Powerfox Local integration.""" + +from __future__ import annotations + +from powerfox import LocalResponse, PowerfoxConnectionError, PowerfoxLocal + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_API_KEY, CONF_HOST +from homeassistant.core import HomeAssistant +from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed + +from .const import DOMAIN, LOGGER, SCAN_INTERVAL + +type PowerfoxLocalConfigEntry = ConfigEntry[PowerfoxLocalDataUpdateCoordinator] + + +class PowerfoxLocalDataUpdateCoordinator(DataUpdateCoordinator[LocalResponse]): + """Class to manage fetching Powerfox local data.""" + + config_entry: PowerfoxLocalConfigEntry + + def __init__(self, hass: HomeAssistant, entry: PowerfoxLocalConfigEntry) -> None: + """Initialize the coordinator.""" + self.client = PowerfoxLocal( + host=entry.data[CONF_HOST], + api_key=entry.data[CONF_API_KEY], + session=async_get_clientsession(hass), + ) + self.device_id: str = entry.data[CONF_API_KEY] + super().__init__( + hass, + LOGGER, + config_entry=entry, + name=f"{DOMAIN}_{entry.data[CONF_HOST]}", + update_interval=SCAN_INTERVAL, + ) + + async def _async_update_data(self) -> LocalResponse: + """Fetch data from the local poweropti.""" + try: + return await self.client.value() + except PowerfoxConnectionError as err: + raise UpdateFailed( + translation_domain=DOMAIN, + translation_key="update_failed", + translation_placeholders={"error": str(err)}, + ) from err diff --git a/homeassistant/components/powerfox_local/entity.py b/homeassistant/components/powerfox_local/entity.py new file mode 100644 index 0000000000000..afa49a6c16c15 --- /dev/null +++ b/homeassistant/components/powerfox_local/entity.py @@ -0,0 +1,28 @@ +"""Base entity for Powerfox Local.""" + +from __future__ import annotations + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import PowerfoxLocalDataUpdateCoordinator + + +class PowerfoxLocalEntity(CoordinatorEntity[PowerfoxLocalDataUpdateCoordinator]): + """Base entity for Powerfox Local.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: PowerfoxLocalDataUpdateCoordinator, + ) -> None: + """Initialize Powerfox Local entity.""" + super().__init__(coordinator) + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, coordinator.device_id)}, + manufacturer="Powerfox", + model="Poweropti", + serial_number=coordinator.device_id, + ) diff --git a/homeassistant/components/powerfox_local/manifest.json b/homeassistant/components/powerfox_local/manifest.json new file mode 100644 index 0000000000000..446e703118822 --- /dev/null +++ b/homeassistant/components/powerfox_local/manifest.json @@ -0,0 +1,17 @@ +{ + "domain": "powerfox_local", + "name": "Powerfox Local", + "codeowners": ["@klaasnicolaas"], + "config_flow": true, + "documentation": "https://www.home-assistant.io/integrations/powerfox_local", + "integration_type": "device", + "iot_class": "local_polling", + "quality_scale": "bronze", + "requirements": ["powerfox==2.1.0"], + "zeroconf": [ + { + "name": "powerfox*", + "type": "_http._tcp.local." + } + ] +} diff --git a/homeassistant/components/powerfox_local/quality_scale.yaml b/homeassistant/components/powerfox_local/quality_scale.yaml new file mode 100644 index 0000000000000..14aef3642918f --- /dev/null +++ b/homeassistant/components/powerfox_local/quality_scale.yaml @@ -0,0 +1,90 @@ +rules: + # Bronze + action-setup: + status: exempt + comment: | + This integration does not provide additional actions. + appropriate-polling: done + brands: done + common-modules: done + config-flow-test-coverage: done + config-flow: done + dependency-transparency: done + docs-actions: + status: exempt + comment: | + This integration does not provide additional actions. + docs-high-level-description: done + docs-installation-instructions: done + docs-removal-instructions: done + entity-event-setup: + status: exempt + comment: | + Entities of this integration does not explicitly subscribe to events. + entity-unique-id: done + has-entity-name: done + runtime-data: done + test-before-configure: done + test-before-setup: done + unique-config-entry: done + + # Silver + action-exceptions: + status: exempt + comment: | + This integration does not provide additional actions. + config-entry-unloading: done + docs-configuration-parameters: + status: exempt + comment: | + This integration does not have an options flow. + docs-installation-parameters: done + entity-unavailable: done + integration-owner: done + log-when-unavailable: done + parallel-updates: done + reauthentication-flow: todo + test-coverage: done + + # Gold + devices: done + diagnostics: todo + discovery-update-info: done + discovery: done + docs-data-update: done + docs-examples: done + docs-known-limitations: done + docs-supported-devices: done + docs-supported-functions: done + docs-troubleshooting: done + docs-use-cases: done + dynamic-devices: + status: exempt + comment: | + Each config entry represents a single device. + entity-category: done + entity-device-class: done + entity-disabled-by-default: + status: exempt + comment: | + There are no entities that should be disabled by default. + entity-translations: done + exception-translations: done + icon-translations: + status: exempt + comment: | + There is no need for icon translations. + reconfiguration-flow: todo + repair-issues: + status: exempt + comment: | + This integration doesn't have any cases where raising an issue is needed. + stale-devices: + status: exempt + comment: | + Each config entry represents a single device. + + # Platinum + async-dependency: done + inject-websession: done + strict-typing: done diff --git a/homeassistant/components/powerfox_local/sensor.py b/homeassistant/components/powerfox_local/sensor.py new file mode 100644 index 0000000000000..10c03c05db2da --- /dev/null +++ b/homeassistant/components/powerfox_local/sensor.py @@ -0,0 +1,112 @@ +"""Sensors for Powerfox Local integration.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass + +from powerfox import LocalResponse + +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorEntity, + SensorEntityDescription, + SensorStateClass, +) +from homeassistant.const import UnitOfEnergy, UnitOfPower +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import PowerfoxLocalConfigEntry, PowerfoxLocalDataUpdateCoordinator +from .entity import PowerfoxLocalEntity + +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class PowerfoxLocalSensorEntityDescription(SensorEntityDescription): + """Describes Powerfox Local sensor entity.""" + + value_fn: Callable[[LocalResponse], float | int | None] + + +SENSORS: tuple[PowerfoxLocalSensorEntityDescription, ...] = ( + PowerfoxLocalSensorEntityDescription( + key="power", + native_unit_of_measurement=UnitOfPower.WATT, + device_class=SensorDeviceClass.POWER, + state_class=SensorStateClass.MEASUREMENT, + value_fn=lambda data: data.power, + ), + PowerfoxLocalSensorEntityDescription( + key="energy_usage", + translation_key="energy_usage", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: data.energy_usage, + ), + PowerfoxLocalSensorEntityDescription( + key="energy_usage_high_tariff", + translation_key="energy_usage_high_tariff", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: data.energy_usage_high_tariff, + ), + PowerfoxLocalSensorEntityDescription( + key="energy_usage_low_tariff", + translation_key="energy_usage_low_tariff", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: data.energy_usage_low_tariff, + ), + PowerfoxLocalSensorEntityDescription( + key="energy_return", + translation_key="energy_return", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL_INCREASING, + value_fn=lambda data: data.energy_return, + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + entry: PowerfoxLocalConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up Powerfox Local sensors based on a config entry.""" + coordinator = entry.runtime_data + + async_add_entities( + PowerfoxLocalSensorEntity( + coordinator=coordinator, + description=description, + ) + for description in SENSORS + if description.value_fn(coordinator.data) is not None + ) + + +class PowerfoxLocalSensorEntity(PowerfoxLocalEntity, SensorEntity): + """Defines a Powerfox Local sensor.""" + + entity_description: PowerfoxLocalSensorEntityDescription + + def __init__( + self, + coordinator: PowerfoxLocalDataUpdateCoordinator, + description: PowerfoxLocalSensorEntityDescription, + ) -> None: + """Initialize the Powerfox Local sensor.""" + super().__init__(coordinator) + self.entity_description = description + self._attr_unique_id = f"{coordinator.device_id}_{description.key}" + + @property + def native_value(self) -> float | int | None: + """Return the state of the entity.""" + return self.entity_description.value_fn(self.coordinator.data) diff --git a/homeassistant/components/powerfox_local/strings.json b/homeassistant/components/powerfox_local/strings.json new file mode 100644 index 0000000000000..db6c06b552410 --- /dev/null +++ b/homeassistant/components/powerfox_local/strings.json @@ -0,0 +1,50 @@ +{ + "config": { + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" + }, + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" + }, + "step": { + "user": { + "data": { + "api_key": "[%key:common::config_flow::data::api_key%]", + "host": "[%key:common::config_flow::data::host%]" + }, + "data_description": { + "api_key": "The API key (device ID) of your Poweropti device.", + "host": "The hostname or IP address of your Poweropti device." + }, + "description": "Set up your Poweropti device to poll locally." + }, + "zeroconf_confirm": { + "description": "Do you want to set up the Poweropti device found at {host}?", + "title": "Discovered Poweropti" + } + } + }, + "entity": { + "sensor": { + "energy_return": { + "name": "Energy return" + }, + "energy_usage": { + "name": "Energy usage" + }, + "energy_usage_high_tariff": { + "name": "Energy usage high tariff" + }, + "energy_usage_low_tariff": { + "name": "Energy usage low tariff" + } + } + }, + "exceptions": { + "update_failed": { + "message": "Error while updating the device: {error}" + } + } +} diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index 03db172602794..d421b58469f6a 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -542,6 +542,7 @@ "poolsense", "portainer", "powerfox", + "powerfox_local", "powerwall", "prana", "private_ble_device", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 118aee2114c6b..b88c7ba291f75 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -5279,9 +5279,20 @@ }, "powerfox": { "name": "Powerfox", - "integration_type": "hub", - "config_flow": true, - "iot_class": "cloud_polling" + "integrations": { + "powerfox": { + "integration_type": "hub", + "config_flow": true, + "iot_class": "cloud_polling", + "name": "Powerfox" + }, + "powerfox_local": { + "integration_type": "device", + "config_flow": true, + "iot_class": "local_polling", + "name": "Powerfox Local" + } + } }, "prana": { "name": "Prana", diff --git a/homeassistant/generated/zeroconf.py b/homeassistant/generated/zeroconf.py index b3b89464d3148..158dc21c8ba64 100644 --- a/homeassistant/generated/zeroconf.py +++ b/homeassistant/generated/zeroconf.py @@ -627,6 +627,10 @@ "domain": "powerfox", "name": "powerfox*", }, + { + "domain": "powerfox_local", + "name": "powerfox*", + }, { "domain": "pure_energie", "name": "smartbridge*", diff --git a/mypy.ini b/mypy.ini index c1fc17cf90843..5d93f1943bed5 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3946,6 +3946,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.powerfox_local.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.powerwall.*] check_untyped_defs = true disallow_incomplete_defs = true diff --git a/requirements_all.txt b/requirements_all.txt index a1e38dfd2fdc4..e4b26c916de60 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1784,6 +1784,7 @@ pmsensor==0.4 poolsense==0.0.8 # homeassistant.components.powerfox +# homeassistant.components.powerfox_local powerfox==2.1.0 # homeassistant.components.prana diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b6b9fb0e346aa..526ce3a1f781a 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1539,6 +1539,7 @@ plugwise==1.11.2 poolsense==0.0.8 # homeassistant.components.powerfox +# homeassistant.components.powerfox_local powerfox==2.1.0 # homeassistant.components.prana diff --git a/tests/components/powerfox_local/__init__.py b/tests/components/powerfox_local/__init__.py new file mode 100644 index 0000000000000..7a4241dffff91 --- /dev/null +++ b/tests/components/powerfox_local/__init__.py @@ -0,0 +1,17 @@ +"""Tests for the Powerfox Local integration.""" + +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + +MOCK_HOST = "1.1.1.1" +MOCK_API_KEY = "9x9x1f12xx3x" +MOCK_DEVICE_ID = MOCK_API_KEY + + +async def setup_integration(hass: HomeAssistant, config_entry: MockConfigEntry) -> None: + """Fixture for setting up the integration.""" + config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/powerfox_local/conftest.py b/tests/components/powerfox_local/conftest.py new file mode 100644 index 0000000000000..272de0c6ff9bc --- /dev/null +++ b/tests/components/powerfox_local/conftest.py @@ -0,0 +1,68 @@ +"""Common fixtures for the Powerfox Local tests.""" + +from collections.abc import Generator +from datetime import UTC, datetime +from unittest.mock import AsyncMock, patch + +from powerfox import LocalResponse +import pytest + +from homeassistant.components.powerfox_local.const import DOMAIN +from homeassistant.const import CONF_API_KEY, CONF_HOST + +from . import MOCK_API_KEY, MOCK_DEVICE_ID, MOCK_HOST + +from tests.common import MockConfigEntry + + +def _local_response() -> LocalResponse: + """Return a mocked local response.""" + return LocalResponse( + timestamp=datetime(2024, 11, 26, 10, 48, 51, tzinfo=UTC), + power=111, + energy_usage=1111111, + energy_return=111111, + energy_usage_high_tariff=111111, + energy_usage_low_tariff=111111, + ) + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Override async_setup_entry.""" + with patch( + "homeassistant.components.powerfox_local.async_setup_entry", return_value=True + ) as mock_setup_entry: + yield mock_setup_entry + + +@pytest.fixture +def mock_powerfox_local_client() -> Generator[AsyncMock]: + """Mock a PowerfoxLocal client.""" + with ( + patch( + "homeassistant.components.powerfox_local.coordinator.PowerfoxLocal", + autospec=True, + ) as mock_client, + patch( + "homeassistant.components.powerfox_local.config_flow.PowerfoxLocal", + new=mock_client, + ), + ): + client = mock_client.return_value + client.value.return_value = _local_response() + yield client + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Mock a Powerfox Local config entry.""" + return MockConfigEntry( + domain=DOMAIN, + title=f"Poweropti ({MOCK_DEVICE_ID[-5:]})", + unique_id=MOCK_DEVICE_ID, + data={ + CONF_HOST: MOCK_HOST, + CONF_API_KEY: MOCK_API_KEY, + }, + ) diff --git a/tests/components/powerfox_local/snapshots/test_sensor.ambr b/tests/components/powerfox_local/snapshots/test_sensor.ambr new file mode 100644 index 0000000000000..96015e04de327 --- /dev/null +++ b/tests/components/powerfox_local/snapshots/test_sensor.ambr @@ -0,0 +1,286 @@ +# serializer version: 1 +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_return-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.poweropti_2xx3x_energy_return', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy return', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy return', + 'platform': 'powerfox_local', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_return', + 'unique_id': '9x9x1f12xx3x_energy_return', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_return-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Poweropti (2xx3x) Energy return', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.poweropti_2xx3x_energy_return', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '111111', + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy usage', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy usage', + 'platform': 'powerfox_local', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_usage', + 'unique_id': '9x9x1f12xx3x_energy_usage', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Poweropti (2xx3x) Energy usage', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1111111', + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage_high_tariff-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage_high_tariff', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy usage high tariff', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy usage high tariff', + 'platform': 'powerfox_local', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_usage_high_tariff', + 'unique_id': '9x9x1f12xx3x_energy_usage_high_tariff', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage_high_tariff-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Poweropti (2xx3x) Energy usage high tariff', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage_high_tariff', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '111111', + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage_low_tariff-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage_low_tariff', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Energy usage low tariff', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Energy usage low tariff', + 'platform': 'powerfox_local', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'energy_usage_low_tariff', + 'unique_id': '9x9x1f12xx3x_energy_usage_low_tariff', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_energy_usage_low_tariff-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'Poweropti (2xx3x) Energy usage low tariff', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.poweropti_2xx3x_energy_usage_low_tariff', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '111111', + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.poweropti_2xx3x_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'powerfox_local', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '9x9x1f12xx3x_power', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.poweropti_2xx3x_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'Poweropti (2xx3x) Power', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.poweropti_2xx3x_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '111', + }) +# --- diff --git a/tests/components/powerfox_local/test_config_flow.py b/tests/components/powerfox_local/test_config_flow.py new file mode 100644 index 0000000000000..65de963b71e1b --- /dev/null +++ b/tests/components/powerfox_local/test_config_flow.py @@ -0,0 +1,186 @@ +"""Test the Powerfox Local config flow.""" + +from unittest.mock import AsyncMock + +from powerfox import PowerfoxAuthenticationError, PowerfoxConnectionError +import pytest + +from homeassistant.components.powerfox_local.const import DOMAIN +from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF +from homeassistant.const import CONF_API_KEY, CONF_HOST +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo + +from . import MOCK_API_KEY, MOCK_DEVICE_ID, MOCK_HOST + +from tests.common import MockConfigEntry + +MOCK_ZEROCONF_DISCOVERY_INFO = ZeroconfServiceInfo( + ip_address=MOCK_HOST, + ip_addresses=[MOCK_HOST], + hostname="powerfox.local", + name="Powerfox", + port=443, + type="_http._tcp", + properties={"id": MOCK_DEVICE_ID}, +) + + +async def test_full_user_flow( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test the full user configuration flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result.get("type") is FlowResultType.FORM + assert result.get("step_id") == "user" + assert not result.get("errors") + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: MOCK_HOST, CONF_API_KEY: MOCK_API_KEY}, + ) + + assert result.get("type") is FlowResultType.CREATE_ENTRY + assert result.get("title") == f"Poweropti ({MOCK_DEVICE_ID[-5:]})" + assert result.get("data") == { + CONF_HOST: MOCK_HOST, + CONF_API_KEY: MOCK_API_KEY, + } + assert result["result"].unique_id == MOCK_DEVICE_ID + assert len(mock_powerfox_local_client.value.mock_calls) == 1 + assert len(mock_setup_entry.mock_calls) == 1 + + +async def test_zeroconf_discovery( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_setup_entry: AsyncMock, +) -> None: + """Test zeroconf discovery.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=MOCK_ZEROCONF_DISCOVERY_INFO, + ) + + assert result.get("type") is FlowResultType.FORM + assert result.get("step_id") == "zeroconf_confirm" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={}, + ) + + assert result.get("type") is FlowResultType.CREATE_ENTRY + assert result.get("title") == f"Poweropti ({MOCK_DEVICE_ID[-5:]})" + assert result.get("data") == { + CONF_HOST: MOCK_HOST, + CONF_API_KEY: MOCK_API_KEY, + } + assert result["result"].unique_id == MOCK_DEVICE_ID + assert len(mock_setup_entry.mock_calls) == 1 + + +@pytest.mark.parametrize( + "exception", + [ + PowerfoxConnectionError, + PowerfoxAuthenticationError, + ], +) +async def test_zeroconf_discovery_errors( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + exception: Exception, +) -> None: + """Test zeroconf discovery aborts on connection/auth errors.""" + mock_powerfox_local_client.value.side_effect = exception + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=MOCK_ZEROCONF_DISCOVERY_INFO, + ) + + assert result.get("type") is FlowResultType.ABORT + assert result.get("reason") == "cannot_connect" + + +async def test_zeroconf_already_configured( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test zeroconf discovery aborts when already configured.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=MOCK_ZEROCONF_DISCOVERY_INFO, + ) + + assert result.get("type") is FlowResultType.ABORT + assert result.get("reason") == "already_configured" + + +async def test_duplicate_entry( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test abort when setting up duplicate entry.""" + mock_config_entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result.get("type") is FlowResultType.FORM + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: MOCK_HOST, CONF_API_KEY: MOCK_API_KEY}, + ) + + assert result.get("type") is FlowResultType.ABORT + assert result.get("reason") == "already_configured" + + +@pytest.mark.parametrize( + ("exception", "error"), + [ + (PowerfoxConnectionError, "cannot_connect"), + (PowerfoxAuthenticationError, "invalid_auth"), + ], +) +async def test_user_flow_exceptions( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_setup_entry: AsyncMock, + exception: Exception, + error: str, +) -> None: + """Test exceptions during user config flow.""" + mock_powerfox_local_client.value.side_effect = exception + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: MOCK_HOST, CONF_API_KEY: MOCK_API_KEY}, + ) + assert result.get("type") is FlowResultType.FORM + assert result.get("errors") == {"base": error} + + # Recover from error + mock_powerfox_local_client.value.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={CONF_HOST: MOCK_HOST, CONF_API_KEY: MOCK_API_KEY}, + ) + assert result.get("type") is FlowResultType.CREATE_ENTRY diff --git a/tests/components/powerfox_local/test_init.py b/tests/components/powerfox_local/test_init.py new file mode 100644 index 0000000000000..e2d71ef7f79e5 --- /dev/null +++ b/tests/components/powerfox_local/test_init.py @@ -0,0 +1,45 @@ +"""Test the Powerfox Local init module.""" + +from __future__ import annotations + +from unittest.mock import AsyncMock + +from powerfox import PowerfoxConnectionError + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant + +from . import setup_integration + +from tests.common import MockConfigEntry + + +async def test_load_unload_entry( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test load and unload entry.""" + await setup_integration(hass, mock_config_entry) + + assert mock_config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + + +async def test_config_entry_not_ready( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test config entry not ready on connection error.""" + mock_powerfox_local_client.value.side_effect = PowerfoxConnectionError + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY diff --git a/tests/components/powerfox_local/test_sensor.py b/tests/components/powerfox_local/test_sensor.py new file mode 100644 index 0000000000000..a5578a5306657 --- /dev/null +++ b/tests/components/powerfox_local/test_sensor.py @@ -0,0 +1,56 @@ +"""Test the sensors provided by the Powerfox Local integration.""" + +from __future__ import annotations + +from datetime import timedelta +from unittest.mock import AsyncMock, patch + +from freezegun.api import FrozenDateTimeFactory +from powerfox import PowerfoxConnectionError +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import STATE_UNAVAILABLE, Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import MOCK_DEVICE_ID, setup_integration + +from tests.common import MockConfigEntry, async_fire_time_changed, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_sensors( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the Powerfox Local sensors.""" + with patch("homeassistant.components.powerfox_local.PLATFORMS", [Platform.SENSOR]): + await setup_integration(hass, mock_config_entry) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_update_failed( + hass: HomeAssistant, + mock_powerfox_local_client: AsyncMock, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test entities become unavailable after failed update.""" + entity_id = f"sensor.poweropti_{MOCK_DEVICE_ID[-5:]}_energy_usage" + await setup_integration(hass, mock_config_entry) + assert mock_config_entry.state is ConfigEntryState.LOADED + + assert hass.states.get(entity_id).state is not None + + mock_powerfox_local_client.value.side_effect = PowerfoxConnectionError + freezer.tick(timedelta(seconds=15)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == STATE_UNAVAILABLE From c7276621eb44d2a1d898d3aee2c725154a2b4633 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Wed, 18 Feb 2026 23:32:23 +0100 Subject: [PATCH 08/20] Add metadata validation for missing backup files in OneDrive backup agent (#163072) --- homeassistant/components/onedrive/backup.py | 15 +++++++++++ .../onedrive_for_business/backup.py | 2 +- tests/components/onedrive/test_backup.py | 25 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/onedrive/backup.py b/homeassistant/components/onedrive/backup.py index 232e8b1ad1242..a76d6df820a77 100644 --- a/homeassistant/components/onedrive/backup.py +++ b/homeassistant/components/onedrive/backup.py @@ -257,9 +257,24 @@ async def _download_metadata(item_id: str) -> AgentBackup | None: ) items = await self._client.list_drive_items(self._folder_id) + + # Build a set of backup filenames to check for orphaned metadata + backup_filenames = { + item.name for item in items if item.name and item.name.endswith(".tar") + } + metadata_files: dict[str, AgentBackup] = {} for item in items: if item.name and item.name.endswith(".metadata.json"): + # Check if corresponding backup file exists + backup_filename = f"{item.name[: -len('.metadata.json')]}.tar" + if backup_filename not in backup_filenames: + _LOGGER.warning( + "Backup file %s not found for metadata %s", + backup_filename, + item.name, + ) + continue if metadata := await _download_metadata(item.id): metadata_files[metadata.backup_id] = metadata diff --git a/homeassistant/components/onedrive_for_business/backup.py b/homeassistant/components/onedrive_for_business/backup.py index 661b616f3cbbc..52ce8af8941cc 100644 --- a/homeassistant/components/onedrive_for_business/backup.py +++ b/homeassistant/components/onedrive_for_business/backup.py @@ -255,7 +255,7 @@ async def _download_metadata(item_id: str) -> AgentBackup | None: for item in items: if item.name and item.name.endswith(".metadata.json"): # Check if corresponding backup file exists - backup_filename = item.name.replace(".metadata.json", ".tar") + backup_filename = f"{item.name[: -len('.metadata.json')]}.tar" if backup_filename not in backup_filenames: _LOGGER.warning( "Backup file %s not found for metadata %s", diff --git a/tests/components/onedrive/test_backup.py b/tests/components/onedrive/test_backup.py index 78e8964bcc80d..d15cfcfa6c60f 100644 --- a/tests/components/onedrive/test_backup.py +++ b/tests/components/onedrive/test_backup.py @@ -157,6 +157,31 @@ async def test_agents_get_backup( } +async def test_agents_get_backup_missing_file( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + mock_config_entry: MockConfigEntry, + mock_onedrive_client: MagicMock, + mock_metadata_file: File, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test what happens when only metadata exists.""" + mock_onedrive_client.list_drive_items.return_value = [mock_metadata_file] + + backup_id = BACKUP_METADATA["backup_id"] + client = await hass_ws_client(hass) + await client.send_json_auto_id({"type": "backup/details", "backup_id": backup_id}) + response = await client.receive_json() + + assert response["success"] + assert response["result"]["agent_errors"] == {} + assert response["result"]["backup"] is None + assert ( + "Backup file 23e64aec.tar not found for metadata 23e64aec.metadata.json" + in caplog.text + ) + + async def test_agents_delete( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, From 3b7b3454d8029937750357c5ad2366c7923a0a81 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Wed, 18 Feb 2026 23:32:39 +0100 Subject: [PATCH 09/20] Simplify ecovacs unload and register teardown before initialize (#163350) --- homeassistant/components/ecovacs/__init__.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/ecovacs/__init__.py b/homeassistant/components/ecovacs/__init__.py index 2e11b96e7d487..9e64dc63c9afd 100644 --- a/homeassistant/components/ecovacs/__init__.py +++ b/homeassistant/components/ecovacs/__init__.py @@ -38,12 +38,11 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: EcovacsConfigEntry) -> bool: """Set up this integration using UI.""" controller = EcovacsController(hass, entry.data) - await controller.initialize() - async def on_unload() -> None: - await controller.teardown() + entry.async_on_unload(controller.teardown) + + await controller.initialize() - entry.async_on_unload(on_unload) entry.runtime_data = controller async def _async_wait_connect(device: VacBot) -> None: From 1fd873869fe097d51f8aada5c76acad99d167609 Mon Sep 17 00:00:00 2001 From: Noah Husby <32528627+noahhusby@users.noreply.github.com> Date: Wed, 18 Feb 2026 16:49:18 -0600 Subject: [PATCH 10/20] Bump aiostreammagic to 2.13.0 (#163408) --- homeassistant/components/cambridge_audio/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/cambridge_audio/manifest.json b/homeassistant/components/cambridge_audio/manifest.json index 445cd2fc60b97..06a1bcb0bc38a 100644 --- a/homeassistant/components/cambridge_audio/manifest.json +++ b/homeassistant/components/cambridge_audio/manifest.json @@ -8,6 +8,6 @@ "iot_class": "local_push", "loggers": ["aiostreammagic"], "quality_scale": "platinum", - "requirements": ["aiostreammagic==2.12.1"], + "requirements": ["aiostreammagic==2.13.0"], "zeroconf": ["_stream-magic._tcp.local.", "_smoip._tcp.local."] } diff --git a/requirements_all.txt b/requirements_all.txt index e4b26c916de60..5b4e4364833d7 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -410,7 +410,7 @@ aiosolaredge==0.2.0 aiosteamist==1.0.1 # homeassistant.components.cambridge_audio -aiostreammagic==2.12.1 +aiostreammagic==2.13.0 # homeassistant.components.switcher_kis aioswitcher==6.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 526ce3a1f781a..5bd52e800e19b 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -395,7 +395,7 @@ aiosolaredge==0.2.0 aiosteamist==1.0.1 # homeassistant.components.cambridge_audio -aiostreammagic==2.12.1 +aiostreammagic==2.13.0 # homeassistant.components.switcher_kis aioswitcher==6.1.0 From 8a1909e5d82320157f59e4ce78bd6d2107f14c0e Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Thu, 19 Feb 2026 08:51:31 +1000 Subject: [PATCH 11/20] Bump hass-splunk to 0.1.4 (#163413) --- homeassistant/components/splunk/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/splunk/manifest.json b/homeassistant/components/splunk/manifest.json index 6407feff8b879..0cbbd5070c1fc 100644 --- a/homeassistant/components/splunk/manifest.json +++ b/homeassistant/components/splunk/manifest.json @@ -7,6 +7,6 @@ "iot_class": "local_push", "loggers": ["hass_splunk"], "quality_scale": "legacy", - "requirements": ["hass-splunk==0.1.1"], + "requirements": ["hass-splunk==0.1.4"], "single_config_entry": true } diff --git a/requirements_all.txt b/requirements_all.txt index 5b4e4364833d7..71eb8041077a0 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1182,7 +1182,7 @@ hanna-cloud==0.0.7 hass-nabucasa==1.15.0 # homeassistant.components.splunk -hass-splunk==0.1.1 +hass-splunk==0.1.4 # homeassistant.components.assist_satellite # homeassistant.components.conversation diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 5bd52e800e19b..2d10f6e08df37 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1052,7 +1052,7 @@ hanna-cloud==0.0.7 hass-nabucasa==1.15.0 # homeassistant.components.splunk -hass-splunk==0.1.1 +hass-splunk==0.1.4 # homeassistant.components.assist_satellite # homeassistant.components.conversation From 14b147b3f718487c0f99305b28af8371ceaae4db Mon Sep 17 00:00:00 2001 From: Brett Adams Date: Thu, 19 Feb 2026 09:11:10 +1000 Subject: [PATCH 12/20] Mark Splunk dependency-transparency quality scale rule as done (#163355) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Claude Haiku 4.5 Co-authored-by: Abílio Costa --- homeassistant/components/splunk/quality_scale.yaml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/homeassistant/components/splunk/quality_scale.yaml b/homeassistant/components/splunk/quality_scale.yaml index 6b736068c7ad2..68403e56ed52a 100644 --- a/homeassistant/components/splunk/quality_scale.yaml +++ b/homeassistant/components/splunk/quality_scale.yaml @@ -13,10 +13,7 @@ rules: config-entry-unloading: done config-flow-test-coverage: done config-flow: done - dependency-transparency: - status: todo - comment: | - The hass-splunk library needs a public CI/CD pipeline. Add GitHub Actions workflow to https://github.com/Bre77/hass_splunk to automate lint, test, build, and publish to PyPI. + dependency-transparency: done docs-actions: status: exempt comment: | From 0f874f7f038662f2043263f759dca47ddf7aea78 Mon Sep 17 00:00:00 2001 From: Joshua Leaper Date: Thu, 19 Feb 2026 09:46:08 +1030 Subject: [PATCH 13/20] Add Config Flow for Ness Alarm (#162414) Co-authored-by: Joostlek Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- CODEOWNERS | 4 +- .../components/ness_alarm/__init__.py | 209 ++++--- .../ness_alarm/alarm_control_panel.py | 46 +- .../components/ness_alarm/binary_sensor.py | 71 ++- .../components/ness_alarm/config_flow.py | 294 +++++++++ homeassistant/components/ness_alarm/const.py | 42 ++ .../components/ness_alarm/manifest.json | 3 +- .../components/ness_alarm/services.py | 53 ++ .../components/ness_alarm/strings.json | 87 +++ homeassistant/generated/config_flows.py | 1 + homeassistant/generated/integrations.json | 2 +- tests/components/ness_alarm/conftest.py | 104 ++++ .../components/ness_alarm/test_config_flow.py | 454 ++++++++++++++ tests/components/ness_alarm/test_init.py | 571 +++++++++++++++--- 14 files changed, 1710 insertions(+), 231 deletions(-) create mode 100644 homeassistant/components/ness_alarm/config_flow.py create mode 100644 homeassistant/components/ness_alarm/const.py create mode 100644 homeassistant/components/ness_alarm/services.py create mode 100644 tests/components/ness_alarm/conftest.py create mode 100644 tests/components/ness_alarm/test_config_flow.py diff --git a/CODEOWNERS b/CODEOWNERS index 17da207490389..109f6ec55c53b 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -1098,8 +1098,8 @@ build.json @home-assistant/supervisor /tests/components/nasweb/ @nasWebio /homeassistant/components/nederlandse_spoorwegen/ @YarmoM @heindrichpaul /tests/components/nederlandse_spoorwegen/ @YarmoM @heindrichpaul -/homeassistant/components/ness_alarm/ @nickw444 -/tests/components/ness_alarm/ @nickw444 +/homeassistant/components/ness_alarm/ @nickw444 @poshy163 +/tests/components/ness_alarm/ @nickw444 @poshy163 /homeassistant/components/nest/ @allenporter /tests/components/nest/ @allenporter /homeassistant/components/netatmo/ @cgtobi diff --git a/homeassistant/components/ness_alarm/__init__.py b/homeassistant/components/ness_alarm/__init__.py index f9ed94a014bf3..4036086fe0fb5 100644 --- a/homeassistant/components/ness_alarm/__init__.py +++ b/homeassistant/components/ness_alarm/__init__.py @@ -1,6 +1,7 @@ """Support for Ness D8X/D16X devices.""" -import datetime +from __future__ import annotations + import logging from typing import NamedTuple @@ -9,41 +10,41 @@ from homeassistant.components.binary_sensor import ( DEVICE_CLASSES_SCHEMA as BINARY_SENSOR_DEVICE_CLASSES_SCHEMA, - BinarySensorDeviceClass, ) +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( - ATTR_CODE, - ATTR_STATE, CONF_HOST, + CONF_PORT, CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_STOP, - Platform, ) -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, Event, HomeAssistant +from homeassistant.data_entry_flow import FlowResultType +from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import async_dispatcher_send +from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from homeassistant.helpers.start import async_at_started from homeassistant.helpers.typing import ConfigType -from homeassistant.util.hass_dict import HassKey - -_LOGGER = logging.getLogger(__name__) -DOMAIN = "ness_alarm" -DATA_NESS: HassKey[Client] = HassKey(DOMAIN) +from .const import ( + CONF_INFER_ARMING_STATE, + CONF_ZONE_ID, + CONF_ZONE_NAME, + CONF_ZONE_TYPE, + CONF_ZONES, + DEFAULT_SCAN_INTERVAL, + DEFAULT_ZONE_TYPE, + DOMAIN, + PLATFORMS, + SIGNAL_ARMING_STATE_CHANGED, + SIGNAL_ZONE_CHANGED, +) +from .services import async_setup_services -CONF_DEVICE_PORT = "port" -CONF_INFER_ARMING_STATE = "infer_arming_state" -CONF_ZONES = "zones" -CONF_ZONE_NAME = "name" -CONF_ZONE_TYPE = "type" -CONF_ZONE_ID = "id" -ATTR_OUTPUT_ID = "output_id" -DEFAULT_SCAN_INTERVAL = datetime.timedelta(minutes=1) -DEFAULT_INFER_ARMING_STATE = False +_LOGGER = logging.getLogger(__name__) -SIGNAL_ZONE_CHANGED = "ness_alarm.zone_changed" -SIGNAL_ARMING_STATE_CHANGED = "ness_alarm.arming_state_changed" +type NessAlarmConfigEntry = ConfigEntry[Client] class ZoneChangedData(NamedTuple): @@ -53,7 +54,6 @@ class ZoneChangedData(NamedTuple): state: bool -DEFAULT_ZONE_TYPE = BinarySensorDeviceClass.MOTION ZONE_SCHEMA = vol.Schema( { vol.Required(CONF_ZONE_NAME): cv.string, @@ -64,88 +64,111 @@ class ZoneChangedData(NamedTuple): } ) +# YAML configuration is deprecated but supported for import CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.Schema( { vol.Required(CONF_HOST): cv.string, - vol.Required(CONF_DEVICE_PORT): cv.port, + vol.Required(CONF_PORT): cv.port, vol.Optional( CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL ): cv.positive_time_period, vol.Optional(CONF_ZONES, default=[]): vol.All( cv.ensure_list, [ZONE_SCHEMA] ), - vol.Optional( - CONF_INFER_ARMING_STATE, default=DEFAULT_INFER_ARMING_STATE - ): cv.boolean, + vol.Optional(CONF_INFER_ARMING_STATE, default=False): cv.boolean, } ) }, extra=vol.ALLOW_EXTRA, ) -SERVICE_PANIC = "panic" -SERVICE_AUX = "aux" - -SERVICE_SCHEMA_PANIC = vol.Schema({vol.Required(ATTR_CODE): cv.string}) -SERVICE_SCHEMA_AUX = vol.Schema( - { - vol.Required(ATTR_OUTPUT_ID): cv.positive_int, - vol.Optional(ATTR_STATE, default=True): cv.boolean, - } -) - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Ness Alarm platform.""" + async_setup_services(hass) + if DOMAIN not in config: + return True - conf = config[DOMAIN] + hass.async_create_task(_async_setup(hass, config)) - zones = conf[CONF_ZONES] - host = conf[CONF_HOST] - port = conf[CONF_DEVICE_PORT] - scan_interval = conf[CONF_SCAN_INTERVAL] - infer_arming_state = conf[CONF_INFER_ARMING_STATE] + return True - client = Client( - host=host, - port=port, - update_interval=scan_interval.total_seconds(), - infer_arming_state=infer_arming_state, - ) - hass.data[DATA_NESS] = client - async def _close(event): - await client.close() +async def _async_setup(hass: HomeAssistant, config: ConfigType) -> None: + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data=config[DOMAIN], + ) + if ( + result.get("type") is FlowResultType.ABORT + and result.get("reason") != "already_configured" + ): + async_create_issue( + hass, + DOMAIN, + f"deprecated_yaml_import_issue_{result.get('reason')}", + breaks_in_ha_version="2026.9.0", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key=f"deprecated_yaml_import_issue_{result.get('reason')}", + translation_placeholders={ + "domain": DOMAIN, + "integration_title": "Ness Alarm", + }, + ) + return + + async_create_issue( + hass, + HOMEASSISTANT_DOMAIN, + f"deprecated_yaml_{DOMAIN}", + breaks_in_ha_version="2026.9.0", + is_fixable=False, + issue_domain=DOMAIN, + severity=IssueSeverity.WARNING, + translation_key="deprecated_yaml", + translation_placeholders={ + "domain": DOMAIN, + "integration_title": "Ness Alarm", + }, + ) - hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) - async def _started(event): - # Force update for current arming status and current zone states (once Home Assistant has finished loading required sensors and panel) - _LOGGER.debug("invoking client keepalive() & update()") - hass.loop.create_task(client.keepalive()) - hass.loop.create_task(client.update()) +async def async_setup_entry(hass: HomeAssistant, entry: NessAlarmConfigEntry) -> bool: + """Set up Ness Alarm from a config entry.""" + client = Client( + host=entry.data[CONF_HOST], + port=entry.data[CONF_PORT], + update_interval=DEFAULT_SCAN_INTERVAL.total_seconds(), + infer_arming_state=entry.data.get(CONF_INFER_ARMING_STATE, False), + ) - async_at_started(hass, _started) + # Verify the client can connect to the alarm panel + try: + await client.update() + except OSError as err: + await client.close() + raise ConfigEntryNotReady( + f"Unable to connect to alarm panel at" + f" {entry.data[CONF_HOST]}:{entry.data[CONF_PORT]}" + ) from err - hass.async_create_task( - async_load_platform( - hass, Platform.BINARY_SENSOR, DOMAIN, {CONF_ZONES: zones}, config - ) - ) - hass.async_create_task( - async_load_platform(hass, Platform.ALARM_CONTROL_PANEL, DOMAIN, {}, config) - ) + entry.runtime_data = client - def on_zone_change(zone_id: int, state: bool): - """Receives and propagates zone state updates.""" + def on_zone_change(zone_id: int, state: bool) -> None: + """Receive and propagate zone state updates.""" async_dispatcher_send( hass, SIGNAL_ZONE_CHANGED, ZoneChangedData(zone_id=zone_id, state=state) ) - def on_state_change(arming_state: ArmingState, arming_mode: ArmingMode | None): - """Receives and propagates arming state updates.""" + def on_state_change( + arming_state: ArmingState, arming_mode: ArmingMode | None + ) -> None: + """Receive and propagate arming state updates.""" async_dispatcher_send( hass, SIGNAL_ARMING_STATE_CHANGED, arming_state, arming_mode ) @@ -153,17 +176,37 @@ def on_state_change(arming_state: ArmingState, arming_mode: ArmingMode | None): client.on_zone_change(on_zone_change) client.on_state_change(on_state_change) - async def handle_panic(call: ServiceCall) -> None: - await client.panic(call.data[ATTR_CODE]) + async def _close(event: Event) -> None: + await client.close() - async def handle_aux(call: ServiceCall) -> None: - await client.aux(call.data[ATTR_OUTPUT_ID], call.data[ATTR_STATE]) + entry.async_on_unload(hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close)) - hass.services.async_register( - DOMAIN, SERVICE_PANIC, handle_panic, schema=SERVICE_SCHEMA_PANIC - ) - hass.services.async_register( - DOMAIN, SERVICE_AUX, handle_aux, schema=SERVICE_SCHEMA_AUX - ) + async def _started(hass: HomeAssistant) -> None: + _LOGGER.debug("Invoking client keepalive() & update()") + hass.async_create_task(client.keepalive()) + hass.async_create_task(client.update()) + + async_at_started(hass, _started) + + # Forward to platforms + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + # Register update listener for options + entry.async_on_unload(entry.add_update_listener(async_reload_entry)) return True + + +async def async_unload_entry(hass: HomeAssistant, entry: NessAlarmConfigEntry) -> bool: + """Unload a config entry.""" + unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + + if unload_ok: + await entry.runtime_data.close() + + return unload_ok + + +async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: + """Reload config entry when options change.""" + await hass.config_entries.async_reload(entry.entry_id) diff --git a/homeassistant/components/ness_alarm/alarm_control_panel.py b/homeassistant/components/ness_alarm/alarm_control_panel.py index 64b764c687262..d9f8d9db3b179 100644 --- a/homeassistant/components/ness_alarm/alarm_control_panel.py +++ b/homeassistant/components/ness_alarm/alarm_control_panel.py @@ -13,11 +13,12 @@ CodeFormat, ) from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect -from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from . import DATA_NESS, SIGNAL_ARMING_STATE_CHANGED +from . import SIGNAL_ARMING_STATE_CHANGED, NessAlarmConfigEntry +from .const import CONF_SHOW_HOME_MODE, DOMAIN _LOGGER = logging.getLogger(__name__) @@ -31,18 +32,18 @@ } -async def async_setup_platform( +async def async_setup_entry( hass: HomeAssistant, - config: ConfigType, - async_add_entities: AddEntitiesCallback, - discovery_info: DiscoveryInfoType | None = None, + entry: NessAlarmConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: - """Set up the Ness Alarm alarm control panel devices.""" - if discovery_info is None: - return + """Set up the Ness Alarm alarm control panel from config entry.""" + client = entry.runtime_data + show_home_mode = entry.options.get(CONF_SHOW_HOME_MODE, True) - device = NessAlarmPanel(hass.data[DATA_NESS], "Alarm Panel") - async_add_entities([device]) + async_add_entities( + [NessAlarmPanel(client, entry.entry_id, show_home_mode)], + ) class NessAlarmPanel(AlarmControlPanelEntity): @@ -50,16 +51,23 @@ class NessAlarmPanel(AlarmControlPanelEntity): _attr_code_format = CodeFormat.NUMBER _attr_should_poll = False - _attr_supported_features = ( - AlarmControlPanelEntityFeature.ARM_HOME - | AlarmControlPanelEntityFeature.ARM_AWAY - | AlarmControlPanelEntityFeature.TRIGGER - ) - def __init__(self, client: Client, name: str) -> None: + def __init__(self, client: Client, entry_id: str, show_home_mode: bool) -> None: """Initialize the alarm panel.""" self._client = client - self._attr_name = name + self._attr_name = "Alarm Panel" + self._attr_unique_id = f"{entry_id}_alarm_panel" + self._attr_device_info = DeviceInfo( + name="Alarm Panel", + identifiers={(DOMAIN, f"{entry_id}_alarm_panel")}, + ) + features = ( + AlarmControlPanelEntityFeature.ARM_AWAY + | AlarmControlPanelEntityFeature.TRIGGER + ) + if show_home_mode: + features |= AlarmControlPanelEntityFeature.ARM_HOME + self._attr_supported_features = features async def async_added_to_hass(self) -> None: """Register callbacks.""" diff --git a/homeassistant/components/ness_alarm/binary_sensor.py b/homeassistant/components/ness_alarm/binary_sensor.py index 8feaa6c696b44..1058f69e37ecd 100644 --- a/homeassistant/components/ness_alarm/binary_sensor.py +++ b/homeassistant/components/ness_alarm/binary_sensor.py @@ -6,41 +6,53 @@ BinarySensorDeviceClass, BinarySensorEntity, ) +from homeassistant.const import CONF_TYPE from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect -from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from . import ( - CONF_ZONE_ID, +from . import SIGNAL_ZONE_CHANGED, NessAlarmConfigEntry, ZoneChangedData +from .const import ( CONF_ZONE_NAME, - CONF_ZONE_TYPE, - CONF_ZONES, - SIGNAL_ZONE_CHANGED, - ZoneChangedData, + CONF_ZONE_NUMBER, + DEFAULT_ZONE_TYPE, + DOMAIN, + SUBENTRY_TYPE_ZONE, ) -async def async_setup_platform( +async def async_setup_entry( hass: HomeAssistant, - config: ConfigType, - async_add_entities: AddEntitiesCallback, - discovery_info: DiscoveryInfoType | None = None, + entry: NessAlarmConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: - """Set up the Ness Alarm binary sensor devices.""" - if not discovery_info: - return + """Set up the Ness Alarm binary sensor from config entry.""" + # Get zone subentries + zone_subentries = filter( + lambda subentry: subentry.subentry_type == SUBENTRY_TYPE_ZONE, + entry.subentries.values(), + ) - configured_zones = discovery_info[CONF_ZONES] + # Create entities from zone subentries + for subentry in zone_subentries: + zone_num: int = subentry.data[CONF_ZONE_NUMBER] + zone_type: BinarySensorDeviceClass = subentry.data.get( + CONF_TYPE, DEFAULT_ZONE_TYPE + ) + zone_name: str | None = subentry.data.get(CONF_ZONE_NAME) - async_add_entities( - NessZoneBinarySensor( - zone_id=zone_config[CONF_ZONE_ID], - name=zone_config[CONF_ZONE_NAME], - zone_type=zone_config[CONF_ZONE_TYPE], + async_add_entities( + [ + NessZoneBinarySensor( + zone_id=zone_num, + zone_type=zone_type, + entry_id=entry.entry_id, + zone_name=zone_name, + ) + ], + config_subentry_id=subentry.subentry_id, ) - for zone_config in configured_zones - ) class NessZoneBinarySensor(BinarySensorEntity): @@ -49,13 +61,22 @@ class NessZoneBinarySensor(BinarySensorEntity): _attr_should_poll = False def __init__( - self, zone_id: int, name: str, zone_type: BinarySensorDeviceClass + self, + zone_id: int, + zone_type: BinarySensorDeviceClass, + entry_id: str, + zone_name: str | None = None, ) -> None: """Initialize the binary_sensor.""" self._zone_id = zone_id - self._attr_name = name self._attr_device_class = zone_type self._attr_is_on = False + self._attr_unique_id = f"{entry_id}_zone_{zone_id}" + self._attr_name = f"Zone {zone_id}" + self._attr_device_info = DeviceInfo( + name=zone_name or f"Zone {zone_id}", + identifiers={(DOMAIN, self._attr_unique_id)}, + ) async def async_added_to_hass(self) -> None: """Register callbacks.""" diff --git a/homeassistant/components/ness_alarm/config_flow.py b/homeassistant/components/ness_alarm/config_flow.py new file mode 100644 index 0000000000000..1cbc11f3320c5 --- /dev/null +++ b/homeassistant/components/ness_alarm/config_flow.py @@ -0,0 +1,294 @@ +"""Config flow for Ness Alarm integration.""" + +from __future__ import annotations + +import asyncio +import logging +from types import MappingProxyType +from typing import Any + +from nessclient import Client +import voluptuous as vol + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass +from homeassistant.config_entries import ( + ConfigEntry, + ConfigFlow, + ConfigFlowResult, + ConfigSubentryData, + ConfigSubentryFlow, + OptionsFlow, + SubentryFlowResult, +) +from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE +from homeassistant.core import callback +from homeassistant.helpers import config_validation as cv, selector + +from .const import ( + CONF_INFER_ARMING_STATE, + CONF_SHOW_HOME_MODE, + CONF_ZONE_ID, + CONF_ZONE_NAME, + CONF_ZONE_NUMBER, + CONF_ZONE_TYPE, + CONF_ZONES, + CONNECTION_TIMEOUT, + DEFAULT_INFER_ARMING_STATE, + DEFAULT_PORT, + DEFAULT_ZONE_TYPE, + DOMAIN, + POST_CONNECTION_DELAY, + SUBENTRY_TYPE_ZONE, +) + +_LOGGER = logging.getLogger(__name__) + +STEP_USER_DATA_SCHEMA = vol.Schema( + { + vol.Required(CONF_HOST): str, + vol.Required(CONF_PORT, default=DEFAULT_PORT): cv.port, + vol.Optional(CONF_INFER_ARMING_STATE, default=DEFAULT_INFER_ARMING_STATE): bool, + } +) + +ZONE_SCHEMA = vol.Schema( + { + vol.Required(CONF_TYPE, default=DEFAULT_ZONE_TYPE): selector.SelectSelector( + selector.SelectSelectorConfig( + options=[cls.value for cls in BinarySensorDeviceClass], + mode=selector.SelectSelectorMode.DROPDOWN, + translation_key="binary_sensor_device_class", + sort=True, + ), + ), + } +) + + +class NessAlarmConfigFlow(ConfigFlow, domain=DOMAIN): + """Handle a config flow for Ness Alarm.""" + + VERSION = 1 + + @classmethod + @callback + def async_get_supported_subentry_types( + cls, config_entry: ConfigEntry + ) -> dict[str, type[ConfigSubentryFlow]]: + """Return subentries supported by this integration.""" + return { + SUBENTRY_TYPE_ZONE: ZoneSubentryFlowHandler, + } + + @staticmethod + @callback + def async_get_options_flow( + config_entry: ConfigEntry, + ) -> OptionsFlow: + """Create the options flow.""" + return NessAlarmOptionsFlowHandler() + + async def _test_connection(self, host: str, port: int) -> None: + """Test connection to the alarm panel. + + Raises OSError on connection failure. + """ + client = Client(host=host, port=port) + try: + await asyncio.wait_for(client.update(), timeout=CONNECTION_TIMEOUT) + except TimeoutError as err: + raise OSError(f"Timed out connecting to {host}:{port}") from err + finally: + await client.close() + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle the initial step.""" + errors: dict[str, str] = {} + + if user_input is not None: + host = user_input[CONF_HOST] + port = user_input[CONF_PORT] + + # Check if already configured + self._async_abort_entries_match({CONF_HOST: host}) + + # Test connection to the alarm panel + try: + await self._test_connection(host, port) + except OSError: + errors["base"] = "cannot_connect" + except Exception: + _LOGGER.exception("Unexpected error connecting to %s:%s", host, port) + errors["base"] = "unknown" + + if not errors: + # Brief delay to ensure the panel releases the test connection + await asyncio.sleep(POST_CONNECTION_DELAY) + return self.async_create_entry( + title=f"Ness Alarm {host}:{port}", + data=user_input, + ) + + return self.async_show_form( + step_id="user", + data_schema=STEP_USER_DATA_SCHEMA, + errors=errors, + ) + + async def async_step_import(self, import_data: dict[str, Any]) -> ConfigFlowResult: + """Import YAML configuration.""" + host = import_data[CONF_HOST] + port = import_data[CONF_PORT] + + # Check if already configured + self._async_abort_entries_match({CONF_HOST: host}) + + # Test connection to the alarm panel + try: + await self._test_connection(host, port) + except OSError: + return self.async_abort(reason="cannot_connect") + except Exception: + _LOGGER.exception( + "Unexpected error connecting to %s:%s during import", host, port + ) + return self.async_abort(reason="unknown") + + # Brief delay to ensure the panel releases the test connection + await asyncio.sleep(POST_CONNECTION_DELAY) + + # Prepare subentries for zones + subentries: list[ConfigSubentryData] = [] + zones = import_data.get(CONF_ZONES, []) + + for zone_config in zones: + zone_id = zone_config[CONF_ZONE_ID] + zone_name = zone_config.get(CONF_ZONE_NAME) + zone_type = zone_config.get(CONF_ZONE_TYPE, DEFAULT_ZONE_TYPE) + + # Subentry title is always "Zone {zone_id}" + title = f"Zone {zone_id}" + + # Build subentry data + subentry_data = { + CONF_ZONE_NUMBER: zone_id, + CONF_TYPE: zone_type, + } + # Include zone name in data if provided (for device naming) + if zone_name: + subentry_data[CONF_ZONE_NAME] = zone_name + + subentries.append( + { + "subentry_type": SUBENTRY_TYPE_ZONE, + "title": title, + "unique_id": f"{SUBENTRY_TYPE_ZONE}_{zone_id}", + "data": MappingProxyType(subentry_data), + } + ) + + return self.async_create_entry( + title=f"Ness Alarm {host}:{port}", + data={ + CONF_HOST: host, + CONF_PORT: port, + CONF_INFER_ARMING_STATE: import_data.get( + CONF_INFER_ARMING_STATE, DEFAULT_INFER_ARMING_STATE + ), + }, + subentries=subentries, + ) + + +class NessAlarmOptionsFlowHandler(OptionsFlow): + """Handle options flow for Ness Alarm.""" + + async def async_step_init( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Manage the options.""" + if user_input is not None: + return self.async_create_entry(title="", data=user_input) + + return self.async_show_form( + step_id="init", + data_schema=self.add_suggested_values_to_schema( + vol.Schema( + { + vol.Required(CONF_SHOW_HOME_MODE, default=True): bool, + } + ), + self.config_entry.options, + ), + ) + + +class ZoneSubentryFlowHandler(ConfigSubentryFlow): + """Handle subentry flow for adding and modifying a zone.""" + + async def async_step_user( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """User flow to add new zone.""" + errors: dict[str, str] = {} + + if user_input is not None: + zone_number = int(user_input[CONF_ZONE_NUMBER]) + unique_id = f"{SUBENTRY_TYPE_ZONE}_{zone_number}" + + # Check if zone already exists + for existing_subentry in self._get_entry().subentries.values(): + if existing_subentry.unique_id == unique_id: + errors[CONF_ZONE_NUMBER] = "already_configured" + + if not errors: + # Store zone_number as int in data + user_input[CONF_ZONE_NUMBER] = zone_number + return self.async_create_entry( + title=f"Zone {zone_number}", + data=user_input, + unique_id=unique_id, + ) + + return self.async_show_form( + step_id="user", + errors=errors, + data_schema=vol.Schema( + { + vol.Required(CONF_ZONE_NUMBER): selector.NumberSelector( + selector.NumberSelectorConfig( + min=1, + max=32, + mode=selector.NumberSelectorMode.BOX, + ) + ), + } + ).extend(ZONE_SCHEMA.schema), + ) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> SubentryFlowResult: + """Reconfigure existing zone.""" + subconfig_entry = self._get_reconfigure_subentry() + + if user_input is not None: + return self.async_update_and_abort( + self._get_entry(), + subconfig_entry, + title=f"Zone {subconfig_entry.data[CONF_ZONE_NUMBER]}", + data_updates=user_input, + ) + + return self.async_show_form( + step_id="reconfigure", + data_schema=self.add_suggested_values_to_schema( + ZONE_SCHEMA, + subconfig_entry.data, + ), + description_placeholders={ + CONF_ZONE_NUMBER: str(subconfig_entry.data[CONF_ZONE_NUMBER]) + }, + ) diff --git a/homeassistant/components/ness_alarm/const.py b/homeassistant/components/ness_alarm/const.py new file mode 100644 index 0000000000000..4503eff282243 --- /dev/null +++ b/homeassistant/components/ness_alarm/const.py @@ -0,0 +1,42 @@ +"""Constants for the Ness Alarm integration.""" + +from datetime import timedelta + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass +from homeassistant.const import Platform + +DOMAIN = "ness_alarm" + +# Platforms +PLATFORMS = [Platform.ALARM_CONTROL_PANEL, Platform.BINARY_SENSOR] + +# Configuration constants +CONF_INFER_ARMING_STATE = "infer_arming_state" +CONF_ZONES = "zones" +CONF_ZONE_NAME = "name" +CONF_ZONE_TYPE = "type" +CONF_ZONE_ID = "id" +CONF_ZONE_NUMBER = "zone_number" +CONF_SHOW_HOME_MODE = "show_home_mode" + +# Subentry types +SUBENTRY_TYPE_ZONE = "zone" + +# Defaults +DEFAULT_PORT = 4999 +DEFAULT_SCAN_INTERVAL = timedelta(minutes=1) +DEFAULT_INFER_ARMING_STATE = False +DEFAULT_ZONE_TYPE = BinarySensorDeviceClass.MOTION + +# Connection +CONNECTION_TIMEOUT = 5 +POST_CONNECTION_DELAY = 1 + +# Signals +SIGNAL_ZONE_CHANGED = "ness_alarm.zone_changed" +SIGNAL_ARMING_STATE_CHANGED = "ness_alarm.arming_state_changed" + +# Services +SERVICE_PANIC = "panic" +SERVICE_AUX = "aux" +ATTR_OUTPUT_ID = "output_id" diff --git a/homeassistant/components/ness_alarm/manifest.json b/homeassistant/components/ness_alarm/manifest.json index 0b032fc24f6b0..600a1430d3734 100644 --- a/homeassistant/components/ness_alarm/manifest.json +++ b/homeassistant/components/ness_alarm/manifest.json @@ -1,7 +1,8 @@ { "domain": "ness_alarm", "name": "Ness Alarm", - "codeowners": ["@nickw444"], + "codeowners": ["@nickw444", "@poshy163"], + "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/ness_alarm", "iot_class": "local_push", "loggers": ["nessclient"], diff --git a/homeassistant/components/ness_alarm/services.py b/homeassistant/components/ness_alarm/services.py new file mode 100644 index 0000000000000..a20c3b7a5d35c --- /dev/null +++ b/homeassistant/components/ness_alarm/services.py @@ -0,0 +1,53 @@ +"""Services for the Ness Alarm integration.""" + +from __future__ import annotations + +import voluptuous as vol + +from homeassistant.const import ATTR_CODE, ATTR_STATE +from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import config_validation as cv + +from .const import ATTR_OUTPUT_ID, DOMAIN, SERVICE_AUX, SERVICE_PANIC + +SERVICE_SCHEMA_PANIC = vol.Schema({vol.Required(ATTR_CODE): cv.string}) +SERVICE_SCHEMA_AUX = vol.Schema( + { + vol.Required(ATTR_OUTPUT_ID): cv.positive_int, + vol.Optional(ATTR_STATE, default=True): cv.boolean, + } +) + + +def async_setup_services(hass: HomeAssistant) -> None: + """Register Ness Alarm services.""" + + async def handle_panic(call: ServiceCall) -> None: + """Handle panic service call.""" + entries = call.hass.config_entries.async_loaded_entries(DOMAIN) + if not entries: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="no_config_entry", + ) + client = entries[0].runtime_data + await client.panic(call.data[ATTR_CODE]) + + async def handle_aux(call: ServiceCall) -> None: + """Handle aux service call.""" + entries = call.hass.config_entries.async_loaded_entries(DOMAIN) + if not entries: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="no_config_entry", + ) + client = entries[0].runtime_data + await client.aux(call.data[ATTR_OUTPUT_ID], call.data[ATTR_STATE]) + + hass.services.async_register( + DOMAIN, SERVICE_PANIC, handle_panic, schema=SERVICE_SCHEMA_PANIC + ) + hass.services.async_register( + DOMAIN, SERVICE_AUX, handle_aux, schema=SERVICE_SCHEMA_AUX + ) diff --git a/homeassistant/components/ness_alarm/strings.json b/homeassistant/components/ness_alarm/strings.json index 94e1cd9a560dd..dea09e2dd6100 100644 --- a/homeassistant/components/ness_alarm/strings.json +++ b/homeassistant/components/ness_alarm/strings.json @@ -1,4 +1,91 @@ { + "config": { + "abort": { + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "error": { + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", + "unknown": "[%key:common::config_flow::error::unknown%]" + }, + "step": { + "user": { + "data": { + "host": "[%key:common::config_flow::data::host%]", + "infer_arming_state": "Infer arming state", + "port": "[%key:common::config_flow::data::port%]" + }, + "data_description": { + "host": "The IP address or hostname of your Ness alarm panel.", + "infer_arming_state": "Attempt to infer the arming state from zone activity.", + "port": "The port on which the Ness alarm panel is accessible." + }, + "description": "Configure connection to your Ness D8X/D16X alarm panel.", + "title": "Set up Ness Alarm" + } + } + }, + "config_subentries": { + "zone": { + "entry_type": "Zone", + "error": { + "already_configured": "Zone with this number is already configured" + }, + "initiate_flow": { + "user": "Add zone" + }, + "step": { + "reconfigure": { + "data": { + "type": "[%key:component::ness_alarm::config_subentries::zone::step::user::data::type%]" + }, + "data_description": { + "type": "[%key:component::ness_alarm::config_subentries::zone::step::user::data_description::type%]" + }, + "title": "Reconfigure zone {zone_number}" + }, + "user": { + "data": { + "type": "Zone type", + "zone_number": "Zone number" + }, + "data_description": { + "type": "Choose the device class you would like the sensor to show as", + "zone_number": "Enter zone number to configure (1-32)" + }, + "title": "Configure zone" + } + } + } + }, + "exceptions": { + "no_config_entry": { + "message": "No Ness Alarm configuration entry is loaded" + } + }, + "issues": { + "deprecated_yaml_import_issue_cannot_connect": { + "description": "Configuring {integration_title} via YAML is deprecated and will be removed in a future release. While importing your configuration, a connection error occurred. Please correct your YAML configuration and restart Home Assistant, or remove the {domain} key from your configuration and configure the integration via the UI.", + "title": "The {integration_title} YAML configuration is being removed" + }, + "deprecated_yaml_import_issue_unknown": { + "description": "Configuring {integration_title} via YAML is deprecated and will be removed in a future release. While importing your configuration, an unknown error occurred. Please correct your YAML configuration and restart Home Assistant, or remove the {domain} key from your configuration and configure the integration via the UI.", + "title": "The {integration_title} YAML configuration is being removed" + } + }, + "options": { + "step": { + "init": { + "data": { + "show_home_mode": "Show arm home mode" + }, + "data_description": { + "show_home_mode": "Enable this to show the arm home option on the alarm panel." + } + } + } + }, "services": { "aux": { "description": "Changes the state of an aux output.", diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index d421b58469f6a..398ebdc31f1f1 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -459,6 +459,7 @@ "nasweb", "neato", "nederlandse_spoorwegen", + "ness_alarm", "nest", "netatmo", "netgear", diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index b88c7ba291f75..03914da84c1b6 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -4481,7 +4481,7 @@ "ness_alarm": { "name": "Ness Alarm", "integration_type": "hub", - "config_flow": false, + "config_flow": true, "iot_class": "local_push" }, "netatmo": { diff --git a/tests/components/ness_alarm/conftest.py b/tests/components/ness_alarm/conftest.py new file mode 100644 index 0000000000000..521416ff9a778 --- /dev/null +++ b/tests/components/ness_alarm/conftest.py @@ -0,0 +1,104 @@ +"""Test fixtures for ness_alarm.""" + +from collections.abc import Generator +from unittest.mock import AsyncMock, MagicMock, patch + +import pytest + +from homeassistant.components.ness_alarm.const import DOMAIN +from homeassistant.const import CONF_HOST, CONF_PORT + +from tests.common import MockConfigEntry + + +class MockClient: + """Mock nessclient.Client stub.""" + + async def panic(self, code): + """Handle panic.""" + + async def disarm(self, code): + """Handle disarm.""" + + async def arm_away(self, code): + """Handle arm_away.""" + + async def arm_home(self, code): + """Handle arm_home.""" + + async def aux(self, output_id, state): + """Handle auxiliary control.""" + + async def keepalive(self): + """Handle keepalive.""" + + async def update(self): + """Handle update.""" + + def on_zone_change(self): + """Handle on_zone_change.""" + + def on_state_change(self): + """Handle on_state_change.""" + + async def close(self): + """Handle close.""" + + +@pytest.fixture +def mock_nessclient(): + """Mock the nessclient Client constructor. + + Replaces nessclient.Client with a Mock which always returns the same + MagicMock() instance. + """ + _mock_instance = MagicMock(MockClient()) + _mock_factory = MagicMock() + _mock_factory.return_value = _mock_instance + + with patch( + "homeassistant.components.ness_alarm.Client", new=_mock_factory, create=True + ): + yield _mock_instance + + +@pytest.fixture +def mock_config_entry() -> MockConfigEntry: + """Return a mock config entry.""" + return MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + + +@pytest.fixture +def mock_client() -> Generator[AsyncMock]: + """Mock the nessclient Client for config flow tests.""" + with patch( + "homeassistant.components.ness_alarm.config_flow.Client", + return_value=AsyncMock(), + ) as mock: + yield mock.return_value + + +@pytest.fixture +def mock_setup_entry() -> Generator[AsyncMock]: + """Mock async_setup_entry.""" + with patch( + "homeassistant.components.ness_alarm.async_setup_entry", + return_value=True, + ) as mock: + yield mock + + +@pytest.fixture(autouse=True) +def post_connection_delay() -> Generator[None]: + """Mock POST_CONNECTION_DELAY to 0 for faster tests.""" + with patch( + "homeassistant.components.ness_alarm.config_flow.POST_CONNECTION_DELAY", + 0, + ): + yield diff --git a/tests/components/ness_alarm/test_config_flow.py b/tests/components/ness_alarm/test_config_flow.py new file mode 100644 index 0000000000000..b738b294c3d9a --- /dev/null +++ b/tests/components/ness_alarm/test_config_flow.py @@ -0,0 +1,454 @@ +"""Test the Ness Alarm config flow.""" + +from types import MappingProxyType +from unittest.mock import AsyncMock + +import pytest + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass +from homeassistant.components.ness_alarm.const import ( + CONF_INFER_ARMING_STATE, + CONF_SHOW_HOME_MODE, + CONF_ZONE_ID, + CONF_ZONE_NAME, + CONF_ZONE_NUMBER, + CONF_ZONE_TYPE, + CONF_ZONES, + DOMAIN, + SUBENTRY_TYPE_ZONE, +) +from homeassistant.config_entries import SOURCE_IMPORT, SOURCE_USER, ConfigSubentry +from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TYPE +from homeassistant.core import HomeAssistant +from homeassistant.data_entry_flow import FlowResultType + +from tests.common import MockConfigEntry + + +async def test_user_flow( + hass: HomeAssistant, mock_client: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test successful user config flow.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: False, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Ness Alarm 192.168.1.100:1992" + assert result["data"] == { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: False, + } + assert len(mock_setup_entry.mock_calls) == 1 + mock_client.close.assert_awaited_once() + + +async def test_user_flow_with_infer_arming_state( + hass: HomeAssistant, mock_client: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test user flow with infer_arming_state enabled.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: True, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["data"][CONF_INFER_ARMING_STATE] is True + + +async def test_user_flow_already_configured( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> None: + """Test we abort if already configured.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: False, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +@pytest.mark.parametrize( + ("side_effect", "expected_error"), + [ + (OSError("Connection refused"), "cannot_connect"), + (TimeoutError, "cannot_connect"), + (RuntimeError("Unexpected"), "unknown"), + ], +) +async def test_user_flow_connection_error_recovery( + hass: HomeAssistant, + mock_client: AsyncMock, + mock_setup_entry: AsyncMock, + side_effect: Exception, + expected_error: str, +) -> None: + """Test connection error handling and recovery.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + # First attempt fails + mock_client.update.side_effect = side_effect + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: False, + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": expected_error} + mock_client.close.assert_awaited_once() + + # Second attempt succeeds + mock_client.update.side_effect = None + mock_client.close.reset_mock() + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + CONF_INFER_ARMING_STATE: False, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + + +async def test_import_yaml_config( + hass: HomeAssistant, mock_client: AsyncMock, mock_setup_entry: AsyncMock +) -> None: + """Test importing YAML configuration.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={ + CONF_HOST: "192.168.1.72", + CONF_PORT: 4999, + CONF_INFER_ARMING_STATE: False, + CONF_ZONES: [ + {CONF_ZONE_NAME: "Garage", CONF_ZONE_ID: 1}, + { + CONF_ZONE_NAME: "Front Door", + CONF_ZONE_ID: 5, + CONF_ZONE_TYPE: BinarySensorDeviceClass.DOOR, + }, + ], + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Ness Alarm 192.168.1.72:4999" + assert result["data"] == { + CONF_HOST: "192.168.1.72", + CONF_PORT: 4999, + CONF_INFER_ARMING_STATE: False, + } + + # Check that subentries were created for zones with names preserved + assert len(result["subentries"]) == 2 + assert result["subentries"][0]["title"] == "Zone 1" + assert result["subentries"][0]["unique_id"] == "zone_1" + assert result["subentries"][0]["data"][CONF_TYPE] == BinarySensorDeviceClass.MOTION + assert result["subentries"][0]["data"][CONF_ZONE_NAME] == "Garage" + assert result["subentries"][1]["title"] == "Zone 5" + assert result["subentries"][1]["unique_id"] == "zone_5" + assert result["subentries"][1]["data"][CONF_TYPE] == BinarySensorDeviceClass.DOOR + assert result["subentries"][1]["data"][CONF_ZONE_NAME] == "Front Door" + + assert len(mock_setup_entry.mock_calls) == 1 + mock_client.close.assert_awaited_once() + + +@pytest.mark.parametrize( + ("side_effect", "expected_reason"), + [ + (OSError("Connection refused"), "cannot_connect"), + (TimeoutError, "cannot_connect"), + (RuntimeError("Unexpected"), "unknown"), + ], +) +async def test_import_yaml_config_errors( + hass: HomeAssistant, + mock_client: AsyncMock, + mock_setup_entry: AsyncMock, + side_effect: Exception, + expected_reason: str, +) -> None: + """Test importing YAML configuration.""" + mock_client.update.side_effect = side_effect + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={ + CONF_HOST: "192.168.1.72", + CONF_PORT: 4999, + CONF_INFER_ARMING_STATE: False, + CONF_ZONES: [ + {CONF_ZONE_NAME: "Garage", CONF_ZONE_ID: 1}, + { + CONF_ZONE_NAME: "Front Door", + CONF_ZONE_ID: 5, + CONF_ZONE_TYPE: BinarySensorDeviceClass.DOOR, + }, + ], + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == expected_reason + + +async def test_import_already_configured( + hass: HomeAssistant, mock_config_entry: MockConfigEntry +) -> None: + """Test we abort import if already configured.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 4999, + CONF_ZONES: [], + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + +@pytest.mark.parametrize( + ("side_effect", "expected_reason"), + [ + (OSError("Connection refused"), "cannot_connect"), + (TimeoutError, "cannot_connect"), + (RuntimeError("Unexpected"), "unknown"), + ], +) +async def test_import_connection_errors( + hass: HomeAssistant, + mock_client: AsyncMock, + side_effect: Exception, + expected_reason: str, +) -> None: + """Test import aborts on connection errors.""" + mock_client.update.side_effect = side_effect + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_IMPORT}, + data={ + CONF_HOST: "192.168.1.72", + CONF_PORT: 4999, + CONF_ZONES: [], + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == expected_reason + mock_client.close.assert_awaited_once() + + +async def test_zone_subentry_flow(hass: HomeAssistant) -> None: + """Test adding a zone through subentry flow.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.subentries.async_init( + (entry.entry_id, SUBENTRY_TYPE_ZONE), + context={"source": SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], + { + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.DOOR, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == "Zone 1" + assert result["data"][CONF_ZONE_NUMBER] == 1 + assert result["data"][CONF_TYPE] == BinarySensorDeviceClass.DOOR + + +async def test_zone_subentry_already_configured(hass: HomeAssistant) -> None: + """Test adding a zone that already exists.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + entry.subentries = { + "zone_1_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data=MappingProxyType( + { + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + } + ), + ) + } + + result = await hass.config_entries.subentries.async_init( + (entry.entry_id, SUBENTRY_TYPE_ZONE), + context={"source": SOURCE_USER}, + ) + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], + { + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.DOOR, + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {CONF_ZONE_NUMBER: "already_configured"} + + +async def test_zone_subentry_reconfigure(hass: HomeAssistant) -> None: + """Test reconfiguring an existing zone.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + zone_subentry = ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data=MappingProxyType( + { + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + } + ), + ) + entry.subentries = {"zone_1_id": zone_subentry} + + result = await entry.start_subentry_reconfigure_flow(hass, "zone_1_id") + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reconfigure" + assert result["description_placeholders"][CONF_ZONE_NUMBER] == "1" + + result = await hass.config_entries.subentries.async_configure( + result["flow_id"], + { + CONF_TYPE: BinarySensorDeviceClass.DOOR, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + + +async def test_options_flow(hass: HomeAssistant) -> None: + """Test options flow to configure alarm panel settings.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "init" + + result = await hass.config_entries.options.async_configure( + result["flow_id"], + { + CONF_SHOW_HOME_MODE: False, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert entry.options[CONF_SHOW_HOME_MODE] is False + + +async def test_options_flow_enable_home_mode(hass: HomeAssistant) -> None: + """Test options flow to enable home mode.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + options={CONF_SHOW_HOME_MODE: False}, + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.options.async_init(entry.entry_id) + result = await hass.config_entries.options.async_configure( + result["flow_id"], + { + CONF_SHOW_HOME_MODE: True, + }, + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert entry.options[CONF_SHOW_HOME_MODE] is True diff --git a/tests/components/ness_alarm/test_init.py b/tests/components/ness_alarm/test_init.py index 48821d3e68dea..eeb5fa30507ee 100644 --- a/tests/components/ness_alarm/test_init.py +++ b/tests/components/ness_alarm/test_init.py @@ -1,26 +1,33 @@ """Tests for the ness_alarm component.""" -from unittest.mock import MagicMock, patch +from types import MappingProxyType +from unittest.mock import AsyncMock, patch from nessclient import ArmingMode, ArmingState -import pytest from homeassistant.components import alarm_control_panel -from homeassistant.components.alarm_control_panel import AlarmControlPanelState -from homeassistant.components.ness_alarm import ( - ATTR_CODE, +from homeassistant.components.alarm_control_panel import ( + AlarmControlPanelEntityFeature, + AlarmControlPanelState, +) +from homeassistant.components.binary_sensor import BinarySensorDeviceClass +from homeassistant.components.ness_alarm.const import ( ATTR_OUTPUT_ID, - CONF_DEVICE_PORT, - CONF_ZONE_ID, - CONF_ZONE_NAME, - CONF_ZONES, + CONF_SHOW_HOME_MODE, + CONF_ZONE_NUMBER, DOMAIN, SERVICE_AUX, SERVICE_PANIC, + SUBENTRY_TYPE_ZONE, ) +from homeassistant.config_entries import ConfigEntryState, ConfigSubentry from homeassistant.const import ( + ATTR_CODE, ATTR_ENTITY_ID, + ATTR_STATE, CONF_HOST, + CONF_PORT, + CONF_TYPE, SERVICE_ALARM_ARM_AWAY, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_DISARM, @@ -28,70 +35,234 @@ STATE_UNKNOWN, ) from homeassistant.core import HomeAssistant +from homeassistant.helpers import issue_registry as ir from homeassistant.setup import async_setup_component -VALID_CONFIG = { - DOMAIN: { - CONF_HOST: "alarm.local", - CONF_DEVICE_PORT: 1234, - CONF_ZONES: [ - {CONF_ZONE_NAME: "Zone 1", CONF_ZONE_ID: 1}, - {CONF_ZONE_NAME: "Zone 2", CONF_ZONE_ID: 2}, - ], - } -} +from tests.common import MockConfigEntry -async def test_setup_platform(hass: HomeAssistant, mock_nessclient) -> None: - """Test platform setup.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) - assert hass.services.has_service(DOMAIN, "panic") - assert hass.services.has_service(DOMAIN, "aux") +async def test_config_entry_setup(hass: HomeAssistant, mock_nessclient) -> None: + """Test config entry setup.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - assert hass.states.get("alarm_control_panel.alarm_panel") is not None - assert hass.states.get("binary_sensor.zone_1") is not None - assert hass.states.get("binary_sensor.zone_2") is not None + # Services should be registered + assert hass.services.has_service(DOMAIN, SERVICE_PANIC) + assert hass.services.has_service(DOMAIN, SERVICE_AUX) + + # Alarm panel should be created + assert hass.states.get("alarm_control_panel.alarm_panel") + + # Client keepalive and update should be called after startup assert mock_nessclient.keepalive.call_count == 1 - assert mock_nessclient.update.call_count == 1 + # update is called once during setup (connection test) and once after startup + assert mock_nessclient.update.call_count == 2 + + +async def test_config_entry_unload(hass: HomeAssistant, mock_nessclient) -> None: + """Test config entry unload.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + + # Client should be closed + mock_nessclient.close.assert_called_once() + + +async def test_config_entry_not_ready(hass: HomeAssistant, mock_nessclient) -> None: + """Test config entry raises ConfigEntryNotReady on connection failure.""" + mock_nessclient.update.side_effect = OSError("Connection refused") + + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + assert entry.state is ConfigEntryState.SETUP_RETRY + mock_nessclient.close.assert_called_once() + + +async def test_config_entry_with_zones(hass: HomeAssistant, mock_nessclient) -> None: + """Test config entry setup with zones as subentries.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + # Add zone subentries + entry.subentries = { + "zone_1_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data={ + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + }, + ), + "zone_2_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_2_id", + unique_id="zone_2", + title="Zone 2", + data={ + CONF_ZONE_NUMBER: 2, + CONF_TYPE: BinarySensorDeviceClass.DOOR, + }, + ), + } + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # Binary sensors should be created for each zone + assert hass.states.get("binary_sensor.zone_1") + assert hass.states.get("binary_sensor.zone_2") + + +async def test_config_entry_reload_on_subentry_add( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test config entry with subentries.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + # Add a zone subentry + entry.subentries = { + "zone_1_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data={ + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + }, + ), + } + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # Zone entity should be created + assert hass.states.get("binary_sensor.zone_1") + + +async def test_panic_service_with_config_entry( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test calling panic service with config entry.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() -async def test_panic_service(hass: HomeAssistant, mock_nessclient) -> None: - """Test calling panic service.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) await hass.services.async_call( DOMAIN, SERVICE_PANIC, blocking=True, service_data={ATTR_CODE: "1234"} ) mock_nessclient.panic.assert_awaited_once_with("1234") -async def test_aux_service(hass: HomeAssistant, mock_nessclient) -> None: - """Test calling aux service.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_aux_service_with_config_entry( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test calling aux service with config entry.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + await hass.services.async_call( DOMAIN, SERVICE_AUX, blocking=True, service_data={ATTR_OUTPUT_ID: 1} ) mock_nessclient.aux.assert_awaited_once_with(1, True) -async def test_dispatch_state_change(hass: HomeAssistant, mock_nessclient) -> None: - """Test calling aux service.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_aux_service_with_state_false( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test calling aux service with state=False.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() - on_state_change = mock_nessclient.on_state_change.call_args[0][0] - on_state_change(ArmingState.ARMING, None) - - await hass.async_block_till_done() - assert hass.states.is_state( - "alarm_control_panel.alarm_panel", AlarmControlPanelState.ARMING + await hass.services.async_call( + DOMAIN, + SERVICE_AUX, + blocking=True, + service_data={ATTR_OUTPUT_ID: 2, ATTR_STATE: False}, ) + mock_nessclient.aux.assert_awaited_once_with(2, False) -async def test_alarm_disarm(hass: HomeAssistant, mock_nessclient) -> None: - """Test disarm.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_alarm_panel_disarm(hass: HomeAssistant, mock_nessclient) -> None: + """Test alarm panel disarm.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() await hass.services.async_call( @@ -106,9 +277,17 @@ async def test_alarm_disarm(hass: HomeAssistant, mock_nessclient) -> None: mock_nessclient.disarm.assert_called_once_with("1234") -async def test_alarm_arm_away(hass: HomeAssistant, mock_nessclient) -> None: - """Test disarm.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_alarm_panel_arm_away(hass: HomeAssistant, mock_nessclient) -> None: + """Test alarm panel arm away.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() await hass.services.async_call( @@ -123,9 +302,17 @@ async def test_alarm_arm_away(hass: HomeAssistant, mock_nessclient) -> None: mock_nessclient.arm_away.assert_called_once_with("1234") -async def test_alarm_arm_home(hass: HomeAssistant, mock_nessclient) -> None: - """Test disarm.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_alarm_panel_arm_home(hass: HomeAssistant, mock_nessclient) -> None: + """Test alarm panel arm home.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() await hass.services.async_call( @@ -140,9 +327,17 @@ async def test_alarm_arm_home(hass: HomeAssistant, mock_nessclient) -> None: mock_nessclient.arm_home.assert_called_once_with("1234") -async def test_alarm_trigger(hass: HomeAssistant, mock_nessclient) -> None: - """Test disarm.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_alarm_panel_trigger(hass: HomeAssistant, mock_nessclient) -> None: + """Test alarm panel trigger.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() await hass.services.async_call( @@ -157,21 +352,79 @@ async def test_alarm_trigger(hass: HomeAssistant, mock_nessclient) -> None: mock_nessclient.panic.assert_called_once_with("1234") -async def test_dispatch_zone_change(hass: HomeAssistant, mock_nessclient) -> None: - """Test zone change events dispatch a signal to subscribers.""" - await async_setup_component(hass, DOMAIN, VALID_CONFIG) +async def test_zone_state_change(hass: HomeAssistant, mock_nessclient) -> None: + """Test zone state change events.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + + # Add zone subentries + entry.subentries = { + "zone_1_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data={ + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + }, + ), + "zone_2_id": ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_2_id", + unique_id="zone_2", + title="Zone 2", + data={ + CONF_ZONE_NUMBER: 2, + CONF_TYPE: BinarySensorDeviceClass.DOOR, + }, + ), + } + + await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() + # Get the zone change callback on_zone_change = mock_nessclient.on_zone_change.call_args[0][0] - on_zone_change(1, True) + # Trigger zone 1 + on_zone_change(1, True) await hass.async_block_till_done() assert hass.states.is_state("binary_sensor.zone_1", "on") - assert hass.states.is_state("binary_sensor.zone_2", "off") + # Trigger zone 2 + on_zone_change(2, True) + await hass.async_block_till_done() + assert hass.states.is_state("binary_sensor.zone_2", "on") + + # Clear zone 1 + on_zone_change(1, False) + await hass.async_block_till_done() + assert hass.states.is_state("binary_sensor.zone_1", "off") + + +async def test_arming_state_changes(hass: HomeAssistant, mock_nessclient) -> None: + """Test all arming state changes.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + # Get the state change callback + on_state_change = mock_nessclient.on_state_change.call_args[0][0] -async def test_arming_state_change(hass: HomeAssistant, mock_nessclient) -> None: - """Test arming state change handing.""" states = [ (ArmingState.UNKNOWN, None, STATE_UNKNOWN), (ArmingState.DISARMED, None, AlarmControlPanelState.DISARMED), @@ -193,67 +446,185 @@ async def test_arming_state_change(hass: HomeAssistant, mock_nessclient) -> None ArmingMode.ARMED_NIGHT, AlarmControlPanelState.ARMED_NIGHT, ), + ( + ArmingState.ARMED, + ArmingMode.ARMED_VACATION, + AlarmControlPanelState.ARMED_VACATION, + ), + ( + ArmingState.ARMED, + ArmingMode.ARMED_DAY, + AlarmControlPanelState.ARMED_AWAY, + ), + ( + ArmingState.ARMED, + ArmingMode.ARMED_HIGHEST, + AlarmControlPanelState.ARMED_AWAY, + ), (ArmingState.ENTRY_DELAY, None, AlarmControlPanelState.PENDING), (ArmingState.TRIGGERED, None, AlarmControlPanelState.TRIGGERED), ] - await async_setup_component(hass, DOMAIN, VALID_CONFIG) - await hass.async_block_till_done() - assert hass.states.is_state("alarm_control_panel.alarm_panel", STATE_UNKNOWN) - on_state_change = mock_nessclient.on_state_change.call_args[0][0] - for arming_state, arming_mode, expected_state in states: on_state_change(arming_state, arming_mode) await hass.async_block_till_done() assert hass.states.is_state("alarm_control_panel.alarm_panel", expected_state) -class MockClient: - """Mock nessclient.Client stub.""" +async def test_arming_state_unknown_mode(hass: HomeAssistant, mock_nessclient) -> None: + """Test arming state with unknown arming mode (for coverage).""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - async def panic(self, code): - """Handle panic.""" + # Get the state change callback + on_state_change = mock_nessclient.on_state_change.call_args[0][0] - async def disarm(self, code): - """Handle disarm.""" + # Test with unhandled arming state (for coverage of warning log) + on_state_change(999, None) # Invalid state + await hass.async_block_till_done() - async def arm_away(self, code): - """Handle arm_away.""" - async def arm_home(self, code): - """Handle arm_home.""" +async def test_homeassistant_stop_event(hass: HomeAssistant, mock_nessclient) -> None: + """Test client is closed on homeassistant_stop event.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - async def aux(self, output_id, state): - """Handle auxiliary control.""" + # Fire the homeassistant_stop event + hass.bus.async_fire("homeassistant_stop") + await hass.async_block_till_done() - async def keepalive(self): - """Handle keepalive.""" + # Client should be closed + mock_nessclient.close.assert_called() - async def update(self): - """Handle update.""" - def on_zone_change(self): - """Handle on_zone_change.""" +async def test_entry_reload_on_update(hass: HomeAssistant, mock_nessclient) -> None: + """Test config entry reload when update listener is triggered.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - def on_state_change(self): - """Handle on_state_change.""" + # Add a zone subentry which should trigger the update listener and reload + zone_subentry = ConfigSubentry( + subentry_type=SUBENTRY_TYPE_ZONE, + subentry_id="zone_1_id", + unique_id="zone_1", + title="Zone 1", + data=MappingProxyType( + { + CONF_ZONE_NUMBER: 1, + CONF_TYPE: BinarySensorDeviceClass.MOTION, + } + ), + ) + hass.config_entries.async_add_subentry(entry, zone_subentry) + await hass.async_block_till_done() - async def close(self): - """Handle close.""" + # Entry should have the new zone subentry + assert len(entry.subentries) == 1 -@pytest.fixture -def mock_nessclient(): - """Mock the nessclient Client constructor. +async def test_alarm_panel_home_mode_disabled( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test alarm panel with home mode disabled via options.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + options={CONF_SHOW_HOME_MODE: False}, + ) + entry.add_to_hass(hass) + + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() - Replaces nessclient.Client with a Mock which always returns the same - MagicMock() instance. - """ - _mock_instance = MagicMock(MockClient()) - _mock_factory = MagicMock() - _mock_factory.return_value = _mock_instance + state = hass.states.get("alarm_control_panel.alarm_panel") + assert state is not None + + # ARM_HOME should not be in supported features + supported = state.attributes["supported_features"] + assert not supported & AlarmControlPanelEntityFeature.ARM_HOME + assert supported & AlarmControlPanelEntityFeature.ARM_AWAY + assert supported & AlarmControlPanelEntityFeature.TRIGGER + + +async def test_alarm_panel_home_mode_enabled_by_default( + hass: HomeAssistant, mock_nessclient +) -> None: + """Test alarm panel has home mode enabled by default.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + }, + ) + entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + state = hass.states.get("alarm_control_panel.alarm_panel") + assert state is not None + + # ARM_HOME should be in supported features by default + supported = state.attributes["supported_features"] + assert supported & AlarmControlPanelEntityFeature.ARM_HOME + assert supported & AlarmControlPanelEntityFeature.ARM_AWAY + assert supported & AlarmControlPanelEntityFeature.TRIGGER + + +async def test_yaml_import_triggers_flow( + hass: HomeAssistant, mock_setup_entry: AsyncMock, issue_registry: ir.IssueRegistry +) -> None: + """Test that YAML configuration triggers import flow.""" with patch( - "homeassistant.components.ness_alarm.Client", new=_mock_factory, create=True + "homeassistant.components.ness_alarm.config_flow.Client", + return_value=AsyncMock(), ): - yield _mock_instance + config = { + DOMAIN: { + CONF_HOST: "192.168.1.100", + CONF_PORT: 1992, + } + } + assert await async_setup_component(hass, DOMAIN, config) + await hass.async_block_till_done() + + # Check that a config entry was created from the import + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + assert entries[0].data[CONF_HOST] == "192.168.1.100" + assert entries[0].data[CONF_PORT] == 1992 + + # Check that a deprecation repair issue was created + issue = issue_registry.async_get_issue( + "homeassistant", f"deprecated_yaml_{DOMAIN}" + ) + assert issue is not None + assert issue.severity == "warning" From e9be363f29bbf5013bed932b82ad5a7fcc00ae4b Mon Sep 17 00:00:00 2001 From: torben-iometer Date: Thu, 19 Feb 2026 00:23:46 +0100 Subject: [PATCH 14/20] add support for multi tariff meter data in iometer (#161767) Co-authored-by: Joostlek --- homeassistant/components/iometer/sensor.py | 16 + homeassistant/components/iometer/strings.json | 6 + tests/components/iometer/__init__.py | 2 +- .../components/iometer/fixtures/reading.json | 2 + .../iometer/snapshots/test_sensor.ambr | 622 ++++++++++++++++++ tests/components/iometer/test_sensor.py | 29 + 6 files changed, 676 insertions(+), 1 deletion(-) create mode 100644 tests/components/iometer/snapshots/test_sensor.ambr create mode 100644 tests/components/iometer/test_sensor.py diff --git a/homeassistant/components/iometer/sensor.py b/homeassistant/components/iometer/sensor.py index 01dc90addfaa0..b83b4a23dd6ae 100644 --- a/homeassistant/components/iometer/sensor.py +++ b/homeassistant/components/iometer/sensor.py @@ -86,6 +86,22 @@ class IOmeterEntityDescription(SensorEntityDescription): options=["entered", "pending", "missing", "unknown"], value_fn=lambda data: data.status.device.core.pin_status or STATE_UNKNOWN, ), + IOmeterEntityDescription( + key="consumption_tariff_t1", + translation_key="consumption_tariff_t1", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL, + value_fn=lambda data: data.reading.get_consumption_tariff_T1(), + ), + IOmeterEntityDescription( + key="consumption_tariff_t2", + translation_key="consumption_tariff_t2", + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + device_class=SensorDeviceClass.ENERGY, + state_class=SensorStateClass.TOTAL, + value_fn=lambda data: data.reading.get_consumption_tariff_T2(), + ), IOmeterEntityDescription( key="total_consumption", translation_key="total_consumption", diff --git a/homeassistant/components/iometer/strings.json b/homeassistant/components/iometer/strings.json index 3c77222bccdb3..a77ff80c6433a 100644 --- a/homeassistant/components/iometer/strings.json +++ b/homeassistant/components/iometer/strings.json @@ -39,6 +39,12 @@ "battery_level": { "name": "Battery level" }, + "consumption_tariff_t1": { + "name": "Consumption Tariff T1" + }, + "consumption_tariff_t2": { + "name": "Consumption Tariff T2" + }, "core_bridge_rssi": { "name": "Signal strength Core/Bridge" }, diff --git a/tests/components/iometer/__init__.py b/tests/components/iometer/__init__.py index 19fe2124f1f79..0daf9cd994448 100644 --- a/tests/components/iometer/__init__.py +++ b/tests/components/iometer/__init__.py @@ -10,7 +10,7 @@ async def setup_platform( hass: HomeAssistant, config_entry: MockConfigEntry, platforms: list[Platform] -) -> MockConfigEntry: +) -> None: """Fixture for setting up the IOmeter platform.""" config_entry.add_to_hass(hass) diff --git a/tests/components/iometer/fixtures/reading.json b/tests/components/iometer/fixtures/reading.json index 82190c88883b0..2b4462290f0ed 100644 --- a/tests/components/iometer/fixtures/reading.json +++ b/tests/components/iometer/fixtures/reading.json @@ -6,6 +6,8 @@ "time": "2024-11-11T11:11:11Z", "registers": [ { "obis": "01-00:01.08.00*ff", "value": 1234.5, "unit": "Wh" }, + { "obis": "01-00:01.08.01*ff", "value": 1904.5, "unit": "Wh" }, + { "obis": "01-00:01.08.02*ff", "value": 9876.21, "unit": "Wh" }, { "obis": "01-00:02.08.00*ff", "value": 5432.1, "unit": "Wh" }, { "obis": "01-00:10.07.00*ff", "value": 100, "unit": "W" } ] diff --git a/tests/components/iometer/snapshots/test_sensor.ambr b/tests/components/iometer/snapshots/test_sensor.ambr new file mode 100644 index 0000000000000..d882bf15acc6d --- /dev/null +++ b/tests/components/iometer/snapshots/test_sensor.ambr @@ -0,0 +1,622 @@ +# serializer version: 1 +# name: test_all_sensors[sensor.iometer_1isk0000000000_battery_level-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_battery_level', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Battery level', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery level', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'battery_level', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_battery_level', + 'unit_of_measurement': '%', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_battery_level-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'IOmeter-1ISK0000000000 Battery level', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_battery_level', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_consumption_tariff_t1-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_consumption_tariff_t1', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Consumption Tariff T1', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Consumption Tariff T1', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'consumption_tariff_t1', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_consumption_tariff_t1', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_consumption_tariff_t1-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'IOmeter-1ISK0000000000 Consumption Tariff T1', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_consumption_tariff_t1', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1904.5', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_consumption_tariff_t2-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_consumption_tariff_t2', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Consumption Tariff T2', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Consumption Tariff T2', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'consumption_tariff_t2', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_consumption_tariff_t2', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_consumption_tariff_t2-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'IOmeter-1ISK0000000000 Consumption Tariff T2', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_consumption_tariff_t2', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '9876.21', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_meter_number-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_meter_number', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Meter number', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:meter-electric', + 'original_name': 'Meter number', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'meter_number', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_meter_number', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_meter_number-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'IOmeter-1ISK0000000000 Meter number', + 'icon': 'mdi:meter-electric', + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_meter_number', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1ISK0000000000', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_pin_status-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'entered', + 'pending', + 'missing', + 'unknown', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_pin_status', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'PIN status', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'PIN status', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'pin_status', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_pin_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_pin_status-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'IOmeter-1ISK0000000000 PIN status', + 'options': list([ + 'entered', + 'pending', + 'missing', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_pin_status', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'entered', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_power-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_power', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_power', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_power-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'power', + 'friendly_name': 'IOmeter-1ISK0000000000 Power', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_power', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_power_supply-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'battery', + 'wired', + 'unknown', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_power_supply', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Power supply', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Power supply', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'power_status', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_power_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_power_supply-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'IOmeter-1ISK0000000000 Power supply', + 'options': list([ + 'battery', + 'wired', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_power_supply', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'battery', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_signal_strength_core_bridge-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.iometer_1isk0000000000_signal_strength_core_bridge', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Signal strength Core/Bridge', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Signal strength Core/Bridge', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'core_bridge_rssi', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_core_bridge_rssi', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_signal_strength_core_bridge-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'IOmeter-1ISK0000000000 Signal strength Core/Bridge', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_signal_strength_core_bridge', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-30', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_signal_strength_wi_fi-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.iometer_1isk0000000000_signal_strength_wi_fi', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Signal strength Wi-Fi', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Signal strength Wi-Fi', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'wifi_rssi', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_wifi_rssi', + 'unit_of_measurement': 'dB', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_signal_strength_wi_fi-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'IOmeter-1ISK0000000000 Signal strength Wi-Fi', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_signal_strength_wi_fi', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '-30', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_total_consumption-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_total_consumption', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total consumption', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total consumption', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'total_consumption', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_total_consumption', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_total_consumption-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'IOmeter-1ISK0000000000 Total consumption', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_total_consumption', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1234.5', + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_total_production-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.iometer_1isk0000000000_total_production', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Total production', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Total production', + 'platform': 'iometer', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'total_production', + 'unique_id': '01JQ6G5395176MAAWKAAPEZHV6_total_production', + 'unit_of_measurement': , + }) +# --- +# name: test_all_sensors[sensor.iometer_1isk0000000000_total_production-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'energy', + 'friendly_name': 'IOmeter-1ISK0000000000 Total production', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.iometer_1isk0000000000_total_production', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '5432.1', + }) +# --- diff --git a/tests/components/iometer/test_sensor.py b/tests/components/iometer/test_sensor.py new file mode 100644 index 0000000000000..4c29a9ff3b48b --- /dev/null +++ b/tests/components/iometer/test_sensor.py @@ -0,0 +1,29 @@ +"""Test the sensors provided by the Powerfox integration.""" + +from __future__ import annotations + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_platform + +from tests.common import MockConfigEntry, snapshot_platform +from tests.components.conftest import AsyncMock + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_all_sensors( + hass: HomeAssistant, + mock_iometer_client: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the Iometer sensors.""" + await setup_platform(hass, mock_config_entry, [Platform.SENSOR]) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) From ca4d537529457133e407b988aa4d56c2d3e33f91 Mon Sep 17 00:00:00 2001 From: elgris <1905821+elgris@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:32:23 +0100 Subject: [PATCH 15/20] Control datetime on SwitchBot Meter Pro CO2 (#161808) --- .../components/switchbot/__init__.py | 6 +- homeassistant/components/switchbot/button.py | 47 ++++++++ .../components/switchbot/strings.json | 3 + tests/components/switchbot/test_button.py | 109 +++++++++++++++++- 4 files changed, 163 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switchbot/__init__.py b/homeassistant/components/switchbot/__init__.py index e24751c9a4010..c002318d6da69 100644 --- a/homeassistant/components/switchbot/__init__.py +++ b/homeassistant/components/switchbot/__init__.py @@ -53,7 +53,11 @@ Platform.SENSOR, ], SupportedModels.HYGROMETER.value: [Platform.SENSOR], - SupportedModels.HYGROMETER_CO2.value: [Platform.SENSOR, Platform.SELECT], + SupportedModels.HYGROMETER_CO2.value: [ + Platform.BUTTON, + Platform.SENSOR, + Platform.SELECT, + ], SupportedModels.CONTACT.value: [Platform.BINARY_SENSOR, Platform.SENSOR], SupportedModels.MOTION.value: [Platform.BINARY_SENSOR, Platform.SENSOR], SupportedModels.PRESENCE_SENSOR.value: [Platform.BINARY_SENSOR, Platform.SENSOR], diff --git a/homeassistant/components/switchbot/button.py b/homeassistant/components/switchbot/button.py index a5a32f96f50f6..3d9db9074f202 100644 --- a/homeassistant/components/switchbot/button.py +++ b/homeassistant/components/switchbot/button.py @@ -5,8 +5,10 @@ import switchbot from homeassistant.components.button import ButtonEntity +from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.util import dt as dt_util from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator from .entity import SwitchbotEntity, exception_handler @@ -31,6 +33,9 @@ async def async_setup_entry( ] ) + if isinstance(coordinator.device, switchbot.SwitchbotMeterProCO2): + async_add_entities([SwitchBotMeterProCO2SyncDateTimeButton(coordinator)]) + class SwitchBotArtFrameButtonBase(SwitchbotEntity, ButtonEntity): """Base class for Art Frame buttons.""" @@ -64,3 +69,45 @@ async def async_press(self) -> None: """Handle the button press.""" _LOGGER.debug("Pressing previous image button %s", self._address) await self._device.prev_image() + + +class SwitchBotMeterProCO2SyncDateTimeButton(SwitchbotEntity, ButtonEntity): + """Button to sync date and time on Meter Pro CO2 to the current HA instance datetime.""" + + _device: switchbot.SwitchbotMeterProCO2 + _attr_entity_category = EntityCategory.CONFIG + _attr_translation_key = "sync_datetime" + + def __init__(self, coordinator: SwitchbotDataUpdateCoordinator) -> None: + """Initialize the sync time button.""" + super().__init__(coordinator) + self._attr_unique_id = f"{coordinator.base_unique_id}_sync_datetime" + + @exception_handler + async def async_press(self) -> None: + """Sync time with Home Assistant.""" + now = dt_util.now() + + # Get UTC offset components + utc_offset = now.utcoffset() + utc_offset_hours, utc_offset_minutes = 0, 0 + if utc_offset is not None: + total_seconds = int(utc_offset.total_seconds()) + utc_offset_hours = total_seconds // 3600 + utc_offset_minutes = abs(total_seconds % 3600) // 60 + + timestamp = int(now.timestamp()) + + _LOGGER.debug( + "Syncing time for %s: timestamp=%s, utc_offset_hours=%s, utc_offset_minutes=%s", + self._address, + timestamp, + utc_offset_hours, + utc_offset_minutes, + ) + + await self._device.set_datetime( + timestamp=timestamp, + utc_offset_hours=utc_offset_hours, + utc_offset_minutes=utc_offset_minutes, + ) diff --git a/homeassistant/components/switchbot/strings.json b/homeassistant/components/switchbot/strings.json index 9c9d36fd319b7..288cc5437e6a8 100644 --- a/homeassistant/components/switchbot/strings.json +++ b/homeassistant/components/switchbot/strings.json @@ -106,6 +106,9 @@ }, "previous_image": { "name": "Previous image" + }, + "sync_datetime": { + "name": "Sync date and time" } }, "climate": { diff --git a/tests/components/switchbot/test_button.py b/tests/components/switchbot/test_button.py index bce9c5f5d5aa9..e01353869b98a 100644 --- a/tests/components/switchbot/test_button.py +++ b/tests/components/switchbot/test_button.py @@ -1,6 +1,7 @@ """Tests for the switchbot button platform.""" from collections.abc import Callable +from datetime import UTC, datetime, timedelta, timezone from unittest.mock import AsyncMock, patch import pytest @@ -8,8 +9,9 @@ from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component -from . import ART_FRAME_INFO +from . import ART_FRAME_INFO, DOMAIN, WOMETERTHPC_SERVICE_INFO from tests.common import MockConfigEntry from tests.components.bluetooth import inject_bluetooth_service_info @@ -60,3 +62,108 @@ async def test_art_frame_button_press( ) mocked_instance.assert_awaited_once() + + +async def test_meter_pro_co2_sync_datetime_button( + hass: HomeAssistant, + mock_entry_factory: Callable[[str], MockConfigEntry], +) -> None: + """Test pressing the sync datetime button on Meter Pro CO2.""" + await async_setup_component(hass, DOMAIN, {}) + inject_bluetooth_service_info(hass, WOMETERTHPC_SERVICE_INFO) + + entry = mock_entry_factory("hygrometer_co2") + entry.add_to_hass(hass) + + mock_set_datetime = AsyncMock(return_value=True) + + # Use a fixed datetime for testing + fixed_time = datetime(2025, 1, 9, 12, 30, 45, tzinfo=UTC) + + with ( + patch( + "switchbot.SwitchbotMeterProCO2.set_datetime", + mock_set_datetime, + ), + patch( + "homeassistant.components.switchbot.button.dt_util.now", + return_value=fixed_time, + ), + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + entity_ids = [ + entity.entity_id for entity in hass.states.async_all(BUTTON_DOMAIN) + ] + assert "button.test_name_sync_date_and_time" in entity_ids + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: "button.test_name_sync_date_and_time"}, + blocking=True, + ) + + mock_set_datetime.assert_awaited_once_with( + timestamp=int(fixed_time.timestamp()), + utc_offset_hours=0, + utc_offset_minutes=0, + ) + + +@pytest.mark.parametrize( + ("tz", "expected_utc_offset_hours", "expected_utc_offset_minutes"), + [ + (timezone(timedelta(hours=0, minutes=0)), 0, 0), + (timezone(timedelta(hours=0, minutes=30)), 0, 30), + (timezone(timedelta(hours=8, minutes=0)), 8, 0), + (timezone(timedelta(hours=-5, minutes=30)), -5, 30), + (timezone(timedelta(hours=5, minutes=30)), 5, 30), + (timezone(timedelta(hours=-5, minutes=-30)), -6, 30), # -6h + 30m = -5:30 + (timezone(timedelta(hours=-5, minutes=-45)), -6, 15), # -6h + 15m = -5:45 + ], +) +async def test_meter_pro_co2_sync_datetime_button_with_timezone( + hass: HomeAssistant, + mock_entry_factory: Callable[[str], MockConfigEntry], + tz: timezone, + expected_utc_offset_hours: int, + expected_utc_offset_minutes: int, +) -> None: + """Test sync datetime button with non-UTC timezone.""" + await async_setup_component(hass, DOMAIN, {}) + inject_bluetooth_service_info(hass, WOMETERTHPC_SERVICE_INFO) + + entry = mock_entry_factory("hygrometer_co2") + entry.add_to_hass(hass) + + mock_set_datetime = AsyncMock(return_value=True) + + fixed_time = datetime(2025, 1, 9, 18, 0, 45, tzinfo=tz) + + with ( + patch( + "switchbot.SwitchbotMeterProCO2.set_datetime", + mock_set_datetime, + ), + patch( + "homeassistant.components.switchbot.button.dt_util.now", + return_value=fixed_time, + ), + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: "button.test_name_sync_date_and_time"}, + blocking=True, + ) + + mock_set_datetime.assert_awaited_once_with( + timestamp=int(fixed_time.timestamp()), + utc_offset_hours=expected_utc_offset_hours, + utc_offset_minutes=expected_utc_offset_minutes, + ) From fafa1935492c5642f3e992264f32b623f9513053 Mon Sep 17 00:00:00 2001 From: Christian Lackas Date: Thu, 19 Feb 2026 00:36:29 +0100 Subject: [PATCH 16/20] Add LED light support for WiredPushButton (HmIPW-WRC2/WRC6) (#161841) Co-authored-by: Joost Lekkerkerker --- .../components/homematicip_cloud/light.py | 141 ++++++++++++++++++ .../components/homematicip_cloud/strings.json | 14 ++ .../fixtures/homematicip_cloud.json | 111 +++++++++++--- .../homematicip_cloud/test_device.py | 2 +- .../homematicip_cloud/test_light.py | 135 +++++++++++++++++ 5 files changed, 382 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/light.py b/homeassistant/components/homematicip_cloud/light.py index e8b0681d059d5..8a68e71cdb9ad 100644 --- a/homeassistant/components/homematicip_cloud/light.py +++ b/homeassistant/components/homematicip_cloud/light.py @@ -22,6 +22,7 @@ PluggableDimmer, SwitchMeasuring, WiredDimmer3, + WiredPushButton, ) from packaging.version import Version @@ -93,6 +94,20 @@ async def async_setup_entry( (Dimmer, PluggableDimmer, BrandDimmer, FullFlushDimmer), ): entities.append(HomematicipDimmer(hap, device)) + elif isinstance(device, WiredPushButton): + optical_channels = sorted( + ( + ch + for ch in device.functionalChannels + if ch.functionalChannelType + == FunctionalChannelType.OPTICAL_SIGNAL_CHANNEL + ), + key=lambda ch: ch.index, + ) + for led_number, ch in enumerate(optical_channels, start=1): + entities.append( + HomematicipOpticalSignalLight(hap, device, ch.index, led_number) + ) async_add_entities(entities) @@ -421,3 +436,129 @@ def _convert_color(color: tuple) -> RGBColorState: if 270 < hue <= 330: return RGBColorState.PURPLE return RGBColorState.RED + + +class HomematicipOpticalSignalLight(HomematicipGenericEntity, LightEntity): + """Representation of HomematicIP WiredPushButton LED light.""" + + _attr_color_mode = ColorMode.HS + _attr_supported_color_modes = {ColorMode.HS} + _attr_supported_features = LightEntityFeature.EFFECT + _attr_translation_key = "optical_signal_light" + + _effect_to_behaviour: dict[str, OpticalSignalBehaviour] = { + "on": OpticalSignalBehaviour.ON, + "blinking": OpticalSignalBehaviour.BLINKING_MIDDLE, + "flash": OpticalSignalBehaviour.FLASH_MIDDLE, + "billow": OpticalSignalBehaviour.BILLOW_MIDDLE, + } + _behaviour_to_effect: dict[OpticalSignalBehaviour, str] = { + v: k for k, v in _effect_to_behaviour.items() + } + + _attr_effect_list = list(_effect_to_behaviour) + + _color_switcher: dict[str, tuple[float, float]] = { + RGBColorState.WHITE: (0.0, 0.0), + RGBColorState.RED: (0.0, 100.0), + RGBColorState.YELLOW: (60.0, 100.0), + RGBColorState.GREEN: (120.0, 100.0), + RGBColorState.TURQUOISE: (180.0, 100.0), + RGBColorState.BLUE: (240.0, 100.0), + RGBColorState.PURPLE: (300.0, 100.0), + } + + def __init__( + self, + hap: HomematicipHAP, + device: WiredPushButton, + channel_index: int, + led_number: int, + ) -> None: + """Initialize the optical signal light entity.""" + super().__init__( + hap, + device, + post=f"LED {led_number}", + channel=channel_index, + is_multi_channel=True, + channel_real_index=channel_index, + ) + + @property + def is_on(self) -> bool: + """Return true if light is on.""" + channel = self.get_channel_or_raise() + return channel.on is True + + @property + def brightness(self) -> int: + """Return the brightness of this light between 0..255.""" + channel = self.get_channel_or_raise() + return int((channel.dimLevel or 0.0) * 255) + + @property + def hs_color(self) -> tuple[float, float]: + """Return the hue and saturation color value [float, float].""" + channel = self.get_channel_or_raise() + simple_rgb_color = channel.simpleRGBColorState + return self._color_switcher.get(simple_rgb_color, (0.0, 0.0)) + + @property + def effect(self) -> str | None: + """Return the current effect.""" + channel = self.get_channel_or_raise() + return self._behaviour_to_effect.get(channel.opticalSignalBehaviour) + + @property + def extra_state_attributes(self) -> dict[str, Any]: + """Return the state attributes of the optical signal light.""" + state_attr = super().extra_state_attributes + channel = self.get_channel_or_raise() + + if self.is_on: + state_attr[ATTR_COLOR_NAME] = channel.simpleRGBColorState + + return state_attr + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn the light on.""" + # Use hs_color from kwargs, if not applicable use current hs_color. + hs_color = kwargs.get(ATTR_HS_COLOR, self.hs_color) + simple_rgb_color = _convert_color(hs_color) + + # If no kwargs, use default value. + brightness = 255 + if ATTR_BRIGHTNESS in kwargs: + brightness = kwargs[ATTR_BRIGHTNESS] + + # Minimum brightness is 10, otherwise the LED is disabled + brightness = max(10, brightness) + dim_level = round(brightness / 255.0, 2) + + effect = self.effect + if ATTR_EFFECT in kwargs: + effect = kwargs[ATTR_EFFECT] + elif effect is None: + effect = "on" + + behaviour = self._effect_to_behaviour.get(effect, OpticalSignalBehaviour.ON) + + await self._device.set_optical_signal_async( + channelIndex=self._channel, + opticalSignalBehaviour=behaviour, + rgb=simple_rgb_color, + dimLevel=dim_level, + ) + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn the light off.""" + channel = self.get_channel_or_raise() + simple_rgb_color = channel.simpleRGBColorState + + await self._device.set_optical_signal_async( + channelIndex=self._channel, + opticalSignalBehaviour=OpticalSignalBehaviour.OFF, + rgb=simple_rgb_color, + dimLevel=0.0, + ) diff --git a/homeassistant/components/homematicip_cloud/strings.json b/homeassistant/components/homematicip_cloud/strings.json index e165a0b9c9110..9e6e5b4e6f50c 100644 --- a/homeassistant/components/homematicip_cloud/strings.json +++ b/homeassistant/components/homematicip_cloud/strings.json @@ -28,6 +28,20 @@ } }, "entity": { + "light": { + "optical_signal_light": { + "state_attributes": { + "effect": { + "state": { + "billow": "Billow", + "blinking": "Blinking", + "flash": "Flash", + "on": "[%key:common::state::on%]" + } + } + } + } + }, "sensor": { "smoke_detector_alarm_counter": { "name": "Alarm counter" diff --git a/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json b/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json index 9f2b4ca38a892..e24f9d284d97f 100644 --- a/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json +++ b/tests/components/homematicip_cloud/fixtures/homematicip_cloud.json @@ -3779,7 +3779,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F711000000000AAAAA25", - "label": "Bewegungsmelder für 55er Rahmen – innen", + "label": "Bewegungsmelder f\u00fcr 55er Rahmen \u2013 innen", "lastStatusUpdate": 1546776387401, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -3841,7 +3841,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000038", - "label": "Weather Sensor – plus", + "label": "Weather Sensor \u2013 plus", "lastStatusUpdate": 1546789939739, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -3958,7 +3958,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000BBBBB1", - "label": "Fußbodenheizungsaktor", + "label": "Fu\u00dfbodenheizungsaktor", "lastStatusUpdate": 1545746610807, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -4110,7 +4110,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F71100000000000BBB17", - "label": "Außen Küche", + "label": "Au\u00dfen K\u00fcche", "lastStatusUpdate": 1546776559553, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -4220,7 +4220,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000000", - "label": "Balkontüre", + "label": "Balkont\u00fcre", "lastStatusUpdate": 1524516526498, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -4439,7 +4439,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000003", - "label": "Küche", + "label": "K\u00fcche", "lastStatusUpdate": 1524514836466, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -4606,7 +4606,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000006", - "label": "Wohnungstüre", + "label": "Wohnungst\u00fcre", "lastStatusUpdate": 1524516489316, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -4946,7 +4946,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000010", - "label": "Büro", + "label": "B\u00fcro", "lastStatusUpdate": 1524513613922, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5101,7 +5101,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000012", - "label": "Heizkörperthermostat", + "label": "Heizk\u00f6rperthermostat", "lastStatusUpdate": 1524514105832, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5154,7 +5154,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000013", - "label": "Heizkörperthermostat2", + "label": "Heizk\u00f6rperthermostat2", "lastStatusUpdate": 1524514007132, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5207,7 +5207,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F71100000000ETRV0013", - "label": "Heizkörperthermostat4", + "label": "Heizk\u00f6rperthermostat4", "lastStatusUpdate": 1524514007132, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5260,7 +5260,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000014", - "label": "Küche-Heizung", + "label": "K\u00fcche-Heizung", "lastStatusUpdate": 1524513898337, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5366,7 +5366,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000016", - "label": "Heizkörperthermostat3", + "label": "Heizk\u00f6rperthermostat3", "lastStatusUpdate": 1524514626157, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -5902,7 +5902,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F7110000000000000029", - "label": "Kontakt-Schnittstelle Unterputz – 1-fach", + "label": "Kontakt-Schnittstelle Unterputz \u2013 1-fach", "lastStatusUpdate": 1547923306429, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -6016,7 +6016,7 @@ }, "homeId": "00000000-0000-0000-0000-000000000001", "id": "3014F711AAAA000000000002", - "label": "Temperatur- und Luftfeuchtigkeitssensor - außen", + "label": "Temperatur- und Luftfeuchtigkeitssensor - au\u00dfen", "lastStatusUpdate": 1524513950325, "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", "manufacturerCode": 1, @@ -7100,7 +7100,7 @@ "groupIndex": 3, "groups": ["00000000-0000-0000-0000-000000000044"], "index": 3, - "label": "Tür", + "label": "T\u00fcr", "multiModeInputMode": "KEY_BEHAVIOR", "supportedOptionalFeatures": { "IOptionalFeatureWindowState": true @@ -9247,6 +9247,77 @@ "serializedGlobalTradeItemNumber": "3014F7110000000000000SB8", "type": "STATUS_BOARD_8", "updateState": "UP_TO_DATE" + }, + "3014F711000000000000WRC6": { + "availableFirmwareVersion": "1.0.0", + "connectionType": "HMIP_WIRED", + "deviceArchetype": "HMIP", + "firmwareVersion": "1.0.0", + "firmwareVersionInteger": 65536, + "functionalChannels": { + "0": { + "configPending": false, + "deviceId": "3014F711000000000000WRC6", + "dutyCycle": false, + "functionalChannelType": "DEVICE_BASE", + "groupIndex": 0, + "groups": [], + "index": 0, + "label": "", + "lowBat": null, + "routerModuleEnabled": false, + "routerModuleSupported": false, + "rssiDeviceValue": -50, + "rssiPeerValue": -52, + "unreach": false, + "supportedOptionalFeatures": {} + }, + "7": { + "deviceId": "3014F711000000000000WRC6", + "dimLevel": 0.5, + "functionalChannelType": "OPTICAL_SIGNAL_CHANNEL", + "groupIndex": 7, + "groups": [], + "index": 7, + "label": "LED 1", + "on": true, + "opticalSignalBehaviour": "ON", + "powerUpSwitchState": "PERMANENT_OFF", + "profileMode": "AUTOMATIC", + "simpleRGBColorState": "GREEN", + "supportedOptionalFeatures": {}, + "userDesiredProfileMode": "AUTOMATIC" + }, + "8": { + "deviceId": "3014F711000000000000WRC6", + "dimLevel": 0.0, + "functionalChannelType": "OPTICAL_SIGNAL_CHANNEL", + "groupIndex": 8, + "groups": [], + "index": 8, + "label": "LED 2", + "on": false, + "opticalSignalBehaviour": "OFF", + "powerUpSwitchState": "PERMANENT_OFF", + "profileMode": "AUTOMATIC", + "simpleRGBColorState": "RED", + "supportedOptionalFeatures": {}, + "userDesiredProfileMode": "AUTOMATIC" + } + }, + "homeId": "00000000-0000-0000-0000-000000000001", + "id": "3014F711000000000000WRC6", + "label": "Wired Taster 6-fach", + "lastStatusUpdate": 1595225686220, + "liveUpdateState": "LIVE_UPDATE_NOT_SUPPORTED", + "manufacturerCode": 1, + "modelId": 400, + "modelType": "HmIPW-WRC6", + "oem": "eQ-3", + "permanentlyReachable": true, + "serializedGlobalTradeItemNumber": "3014F711000000000000WRC6", + "type": "WIRED_PUSH_BUTTON_6", + "updateState": "UP_TO_DATE" } }, "groups": { @@ -9525,7 +9596,7 @@ "humidityLimitEnabled": true, "humidityLimitValue": 60, "id": "00000000-0000-0000-0000-000000000010", - "label": "Büro", + "label": "B\u00fcro", "lastSetPointReachedTimestamp": 1557767559939, "lastSetPointUpdatedTimestamp": 1557767559939, "lastStatusUpdate": 1524516454116, @@ -9642,7 +9713,7 @@ "dutyCycle": false, "homeId": "00000000-0000-0000-0000-000000000001", "id": "00000000-0000-0000-0000-000000000009", - "label": "Büro", + "label": "B\u00fcro", "lastStatusUpdate": 1524515854304, "lowBat": false, "metaGroupId": "00000000-0000-0000-0000-000000000008", @@ -10008,7 +10079,7 @@ "homeId": "00000000-0000-0000-0000-000000000001", "id": "00000000-0000-0000-0000-000000000008", "incorrectPositioned": null, - "label": "Büro", + "label": "B\u00fcro", "lastStatusUpdate": 1524516454116, "lowBat": false, "metaGroupId": null, @@ -11065,7 +11136,7 @@ "inboxGroup": "00000000-0000-0000-0000-000000000044", "lastReadyForUpdateTimestamp": 1522319489138, "location": { - "city": "1010 Wien, Österreich", + "city": "1010 Wien, \u00d6sterreich", "latitude": "48.208088", "longitude": "16.358608" }, diff --git a/tests/components/homematicip_cloud/test_device.py b/tests/components/homematicip_cloud/test_device.py index 0722047327d7d..6abc1ef36851d 100644 --- a/tests/components/homematicip_cloud/test_device.py +++ b/tests/components/homematicip_cloud/test_device.py @@ -22,7 +22,7 @@ async def test_hmip_load_all_supported_devices( test_devices=None, test_groups=None ) - assert len(mock_hap.hmip_device_by_entity_id) == 346 + assert len(mock_hap.hmip_device_by_entity_id) == 348 async def test_hmip_remove_device( diff --git a/tests/components/homematicip_cloud/test_light.py b/tests/components/homematicip_cloud/test_light.py index be432eaae3150..21a80504665d9 100644 --- a/tests/components/homematicip_cloud/test_light.py +++ b/tests/components/homematicip_cloud/test_light.py @@ -676,3 +676,138 @@ async def test_hmip_light_hs( "saturation_level": hmip_device.functionalChannels[1].saturationLevel, "dim_level": 0.16, } + + +async def test_hmip_wired_push_button_led( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test HomematicipOpticalSignalLight.""" + entity_id = "light.led_1" + entity_name = "LED 1" + device_model = "HmIPW-WRC6" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=["Wired Taster 6-fach"] + ) + + ha_state, hmip_device = get_and_check_entity_basics( + hass, mock_hap, entity_id, entity_name, device_model + ) + + assert ha_state.state == STATE_ON + assert ha_state.attributes[ATTR_COLOR_MODE] == ColorMode.HS + assert ha_state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.HS] + assert ha_state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.EFFECT + assert ha_state.attributes[ATTR_BRIGHTNESS] == 127 + assert ha_state.attributes[ATTR_COLOR_NAME] == "GREEN" + + service_call_counter = len(hmip_device.mock_calls) + + # Test turning on with color and brightness + await hass.services.async_call( + "light", + "turn_on", + {"entity_id": entity_id, ATTR_HS_COLOR: [240.0, 100.0], ATTR_BRIGHTNESS: 128}, + blocking=True, + ) + assert hmip_device.mock_calls[-1][0] == "set_optical_signal_async" + assert hmip_device.mock_calls[-1][2] == { + "channelIndex": 7, + "opticalSignalBehaviour": OpticalSignalBehaviour.ON, + "rgb": "BLUE", + "dimLevel": 0.5, + } + assert len(hmip_device.mock_calls) == service_call_counter + 1 + + # Test turning on with effect + await hass.services.async_call( + "light", + "turn_on", + {"entity_id": entity_id, ATTR_EFFECT: "blinking"}, + blocking=True, + ) + assert hmip_device.mock_calls[-1][0] == "set_optical_signal_async" + assert ( + hmip_device.mock_calls[-1][2]["opticalSignalBehaviour"] + == OpticalSignalBehaviour.BLINKING_MIDDLE + ) + assert len(hmip_device.mock_calls) == service_call_counter + 2 + + +async def test_hmip_wired_push_button_led_turn_off( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test HomematicipOpticalSignalLight turn off.""" + entity_id = "light.led_1" + entity_name = "LED 1" + device_model = "HmIPW-WRC6" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=["Wired Taster 6-fach"] + ) + + ha_state, hmip_device = get_and_check_entity_basics( + hass, mock_hap, entity_id, entity_name, device_model + ) + + assert ha_state.state == STATE_ON + + service_call_counter = len(hmip_device.mock_calls) + + # Test turning off + await hass.services.async_call( + "light", + "turn_off", + {"entity_id": entity_id}, + blocking=True, + ) + assert hmip_device.mock_calls[-1][0] == "set_optical_signal_async" + assert hmip_device.mock_calls[-1][2] == { + "channelIndex": 7, + "opticalSignalBehaviour": OpticalSignalBehaviour.OFF, + "rgb": "GREEN", + "dimLevel": 0.0, + } + assert len(hmip_device.mock_calls) == service_call_counter + 1 + + # Verify state after turning off + await async_manipulate_test_data( + hass, hmip_device, "on", False, channel_real_index=7 + ) + await async_manipulate_test_data( + hass, hmip_device, "dimLevel", 0.0, channel_real_index=7 + ) + ha_state = hass.states.get(entity_id) + assert ha_state.state == STATE_OFF + + +async def test_hmip_wired_push_button_led_2( + hass: HomeAssistant, default_mock_hap_factory: HomeFactory +) -> None: + """Test HomematicipOpticalSignalLight second LED.""" + entity_id = "light.led_2" + entity_name = "LED 2" + device_model = "HmIPW-WRC6" + mock_hap = await default_mock_hap_factory.async_get_mock_hap( + test_devices=["Wired Taster 6-fach"] + ) + + ha_state, hmip_device = get_and_check_entity_basics( + hass, mock_hap, entity_id, entity_name, device_model + ) + + assert ha_state.state == STATE_OFF + assert ha_state.attributes[ATTR_COLOR_MODE] is None + assert ha_state.attributes[ATTR_SUPPORTED_COLOR_MODES] == [ColorMode.HS] + assert ha_state.attributes[ATTR_SUPPORTED_FEATURES] == LightEntityFeature.EFFECT + + service_call_counter = len(hmip_device.mock_calls) + + # Test turning on second LED + await hass.services.async_call( + "light", + "turn_on", + {"entity_id": entity_id}, + blocking=True, + ) + assert hmip_device.mock_calls[-1][0] == "set_optical_signal_async" + assert hmip_device.mock_calls[-1][2]["channelIndex"] == 8 + assert len(hmip_device.mock_calls) == service_call_counter + 1 From cd5775ca35a6d59891ad05c1345181ade8f12338 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Thu, 19 Feb 2026 00:37:17 +0100 Subject: [PATCH 17/20] Add integration_type service to simplepush (#163394) --- homeassistant/components/simplepush/manifest.json | 1 + homeassistant/generated/integrations.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/simplepush/manifest.json b/homeassistant/components/simplepush/manifest.json index 5b792072f4479..54b55475465a9 100644 --- a/homeassistant/components/simplepush/manifest.json +++ b/homeassistant/components/simplepush/manifest.json @@ -4,6 +4,7 @@ "codeowners": ["@engrbm87"], "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/simplepush", + "integration_type": "service", "iot_class": "cloud_polling", "loggers": ["simplepush"], "requirements": ["simplepush==2.2.3"] diff --git a/homeassistant/generated/integrations.json b/homeassistant/generated/integrations.json index 03914da84c1b6..546a664ac5737 100644 --- a/homeassistant/generated/integrations.json +++ b/homeassistant/generated/integrations.json @@ -6192,7 +6192,7 @@ }, "simplepush": { "name": "Simplepush", - "integration_type": "hub", + "integration_type": "service", "config_flow": true, "iot_class": "cloud_polling" }, From b398197c075b06e4a30d665e8fb90e3e52864d75 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Thu, 19 Feb 2026 00:46:06 +0100 Subject: [PATCH 18/20] Debug logging for config_entries (#163378) --- homeassistant/config_entries.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index a89c5869a2faf..1fb4c2785fe1f 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -798,6 +798,7 @@ async def __async_setup_with_context( self.domain, auth_message, ) + _LOGGER.debug("Full exception", exc_info=True) self.async_start_reauth(hass) except ConfigEntryNotReady as exc: message = str(exc) @@ -815,13 +816,14 @@ async def __async_setup_with_context( ) self._tries += 1 ready_message = f"ready yet: {message}" if message else "ready yet" - _LOGGER.debug( + _LOGGER.info( "Config entry '%s' for %s integration not %s; Retrying in %d seconds", self.title, self.domain, ready_message, wait_time, ) + _LOGGER.debug("Full exception", exc_info=True) if hass.state is CoreState.running: self._async_cancel_retry_setup = async_call_later( From 2fcbd77c9528feaeb98d8ccac6474151f35e6ea2 Mon Sep 17 00:00:00 2001 From: Manu <4445816+tr4nt0r@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:48:01 +0100 Subject: [PATCH 19/20] Don't set last notification timestamp when sending message failed (#163251) --- homeassistant/components/notify/__init__.py | 2 +- tests/components/notify/test_init.py | 59 +++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/notify/__init__.py b/homeassistant/components/notify/__init__.py index 97759db4c1324..e18fced8f8a8c 100644 --- a/homeassistant/components/notify/__init__.py +++ b/homeassistant/components/notify/__init__.py @@ -161,9 +161,9 @@ async def _async_send_message(self, **kwargs: Any) -> None: Should not be overridden, handle setting last notification timestamp. """ + await self.async_send_message(**kwargs) self.__set_state(dt_util.utcnow().isoformat()) self.async_write_ha_state() - await self.async_send_message(**kwargs) def send_message(self, message: str, title: str | None = None) -> None: """Send a message.""" diff --git a/tests/components/notify/test_init.py b/tests/components/notify/test_init.py index 16a583fdf5ca6..f6a9f39a6e856 100644 --- a/tests/components/notify/test_init.py +++ b/tests/components/notify/test_init.py @@ -17,6 +17,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant, State +from homeassistant.exceptions import HomeAssistantError from tests.common import ( MockConfigEntry, @@ -52,6 +53,17 @@ def send_message(self, message: str, title: str | None = None) -> None: self.send_message_mock_calls(message, title=title) +class MockNotifyEntityWithException(MockEntity, NotifyEntity): + """Mock Email notitier entity to use in tests.""" + + send_message_mock_calls = MagicMock() + + async def async_send_message(self, message: str, title: str | None = None) -> None: + """Send a notification message.""" + self.send_message_mock_calls(message, title=title) + raise HomeAssistantError + + async def help_async_setup_entry_init( hass: HomeAssistant, config_entry: ConfigEntry ) -> bool: @@ -188,6 +200,53 @@ async def test_send_message_service_with_title( ) +async def test_send_message_exception( + hass: HomeAssistant, config_flow_fixture: None +) -> None: + """Test send_message service.""" + + entity = MockNotifyEntityWithException( + name="test", + entity_id="notify.test", + supported_features=NotifyEntityFeature.TITLE, + ) + + config_entry = MockConfigEntry(domain="test") + config_entry.add_to_hass(hass) + + mock_integration( + hass, + MockModule( + "test", + async_setup_entry=help_async_setup_entry_init, + async_unload_entry=help_async_unload_entry, + ), + ) + setup_test_component_platform(hass, DOMAIN, [entity], from_config_entry=True) + assert await hass.config_entries.async_setup(config_entry.entry_id) + + state = hass.states.get("notify.test") + assert state.state is STATE_UNKNOWN + + with pytest.raises(HomeAssistantError): + await hass.services.async_call( + DOMAIN, + SERVICE_SEND_MESSAGE, + copy.deepcopy(TEST_KWARGS_TITLE) | {"entity_id": "notify.test"}, + blocking=True, + ) + await hass.async_block_till_done() + + entity.send_message_mock_calls.assert_called_once_with( + TEST_KWARGS_TITLE[notify.ATTR_MESSAGE], + title=TEST_KWARGS_TITLE[notify.ATTR_TITLE], + ) + + # assert last notification timestamp has not been updated + state = hass.states.get("notify.test") + assert state.state is STATE_UNKNOWN + + @pytest.mark.parametrize( ("state", "init_state"), [ From 37f0f1869f5a564813da98150f7e9d71ac1c87d2 Mon Sep 17 00:00:00 2001 From: rhcp011235 Date: Wed, 18 Feb 2026 19:02:43 -0500 Subject: [PATCH 20/20] Add sleep health metrics to SleepIQ integration (#163403) Co-authored-by: Claude Sonnet 4.6 --- homeassistant/components/sleepiq/__init__.py | 4 + homeassistant/components/sleepiq/const.py | 10 + .../components/sleepiq/coordinator.py | 43 +- homeassistant/components/sleepiq/entity.py | 14 +- homeassistant/components/sleepiq/icons.json | 21 + homeassistant/components/sleepiq/sensor.py | 93 ++- tests/components/sleepiq/conftest.py | 15 + .../sleepiq/snapshots/test_sensor.ambr | 556 ++++++++++++++++++ 8 files changed, 744 insertions(+), 12 deletions(-) create mode 100644 homeassistant/components/sleepiq/icons.json diff --git a/homeassistant/components/sleepiq/__init__.py b/homeassistant/components/sleepiq/__init__.py index 565611fe1692c..8eb703b7f5f3e 100644 --- a/homeassistant/components/sleepiq/__init__.py +++ b/homeassistant/components/sleepiq/__init__.py @@ -26,6 +26,7 @@ SleepIQData, SleepIQDataUpdateCoordinator, SleepIQPauseUpdateCoordinator, + SleepIQSleepDataCoordinator, ) _LOGGER = logging.getLogger(__name__) @@ -96,14 +97,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: coordinator = SleepIQDataUpdateCoordinator(hass, entry, gateway) pause_coordinator = SleepIQPauseUpdateCoordinator(hass, entry, gateway) + sleep_data_coordinator = SleepIQSleepDataCoordinator(hass, entry, gateway) # Call the SleepIQ API to refresh data await coordinator.async_config_entry_first_refresh() await pause_coordinator.async_config_entry_first_refresh() + await sleep_data_coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {})[entry.entry_id] = SleepIQData( data_coordinator=coordinator, pause_coordinator=pause_coordinator, + sleep_data_coordinator=sleep_data_coordinator, client=gateway, ) diff --git a/homeassistant/components/sleepiq/const.py b/homeassistant/components/sleepiq/const.py index 7a9415bac20f1..0efb8e94ebe56 100644 --- a/homeassistant/components/sleepiq/const.py +++ b/homeassistant/components/sleepiq/const.py @@ -15,6 +15,11 @@ SLEEP_NUMBER = "sleep_number" FOOT_WARMING_TIMER = "foot_warming_timer" FOOT_WARMER = "foot_warmer" +SLEEP_SCORE = "sleep_score" +SLEEP_DURATION = "sleep_duration" +HEART_RATE = "heart_rate" +RESPIRATORY_RATE = "respiratory_rate" +HRV = "hrv" ENTITY_TYPES = { ACTUATOR: "Position", CORE_CLIMATE_TIMER: "Core Climate Timer", @@ -25,6 +30,11 @@ SLEEP_NUMBER: "SleepNumber", FOOT_WARMING_TIMER: "Foot Warming Timer", FOOT_WARMER: "Foot Warmer", + SLEEP_SCORE: "Sleep Score", + SLEEP_DURATION: "Sleep Duration", + HEART_RATE: "Heart Rate Average", + RESPIRATORY_RATE: "Respiratory Rate Average", + HRV: "Heart Rate Variability", } LEFT = "left" diff --git a/homeassistant/components/sleepiq/coordinator.py b/homeassistant/components/sleepiq/coordinator.py index 46b754976e58b..0baeca03fe560 100644 --- a/homeassistant/components/sleepiq/coordinator.py +++ b/homeassistant/components/sleepiq/coordinator.py @@ -5,17 +5,18 @@ from datetime import timedelta import logging -from asyncsleepiq import AsyncSleepIQ +from asyncsleepiq import AsyncSleepIQ, SleepIQAPIException, SleepIQTimeoutException from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.helpers.update_coordinator import DataUpdateCoordinator +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed _LOGGER = logging.getLogger(__name__) UPDATE_INTERVAL = timedelta(seconds=60) LONGER_UPDATE_INTERVAL = timedelta(minutes=5) +SLEEP_DATA_UPDATE_INTERVAL = timedelta(hours=1) # Sleep data doesn't change frequently class SleepIQDataUpdateCoordinator(DataUpdateCoordinator[None]): @@ -74,10 +75,48 @@ async def _async_update_data(self) -> None: ) +class SleepIQSleepDataCoordinator(DataUpdateCoordinator[None]): + """SleepIQ sleep health data coordinator.""" + + config_entry: ConfigEntry + + def __init__( + self, + hass: HomeAssistant, + config_entry: ConfigEntry, + client: AsyncSleepIQ, + ) -> None: + """Initialize coordinator.""" + super().__init__( + hass, + _LOGGER, + config_entry=config_entry, + name=f"{config_entry.data[CONF_USERNAME]}@SleepIQSleepData", + update_interval=SLEEP_DATA_UPDATE_INTERVAL, + ) + self.client = client + + async def _async_update_data(self) -> None: + """Fetch sleep health data from API via asyncsleepiq library.""" + try: + await asyncio.gather( + *[ + sleeper.fetch_sleep_data() + for bed in self.client.beds.values() + for sleeper in bed.sleepers + ] + ) + except SleepIQTimeoutException as err: + raise UpdateFailed(f"Timed out fetching SleepIQ sleep data: {err}") from err + except SleepIQAPIException as err: + raise UpdateFailed(f"Failed to fetch SleepIQ sleep data: {err}") from err + + @dataclass class SleepIQData: """Data for the sleepiq integration.""" data_coordinator: SleepIQDataUpdateCoordinator pause_coordinator: SleepIQPauseUpdateCoordinator + sleep_data_coordinator: SleepIQSleepDataCoordinator client: AsyncSleepIQ diff --git a/homeassistant/components/sleepiq/entity.py b/homeassistant/components/sleepiq/entity.py index 829e3a00e6fd6..49d58b7d5e1a2 100644 --- a/homeassistant/components/sleepiq/entity.py +++ b/homeassistant/components/sleepiq/entity.py @@ -11,9 +11,17 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import ENTITY_TYPES, ICON_OCCUPIED -from .coordinator import SleepIQDataUpdateCoordinator, SleepIQPauseUpdateCoordinator - -type _DataCoordinatorType = SleepIQDataUpdateCoordinator | SleepIQPauseUpdateCoordinator +from .coordinator import ( + SleepIQDataUpdateCoordinator, + SleepIQPauseUpdateCoordinator, + SleepIQSleepDataCoordinator, +) + +type _DataCoordinatorType = ( + SleepIQDataUpdateCoordinator + | SleepIQPauseUpdateCoordinator + | SleepIQSleepDataCoordinator +) def device_from_bed(bed: SleepIQBed) -> DeviceInfo: diff --git a/homeassistant/components/sleepiq/icons.json b/homeassistant/components/sleepiq/icons.json new file mode 100644 index 0000000000000..6a3534e325640 --- /dev/null +++ b/homeassistant/components/sleepiq/icons.json @@ -0,0 +1,21 @@ +{ + "entity": { + "sensor": { + "heart_rate_avg": { + "default": "mdi:heart-pulse" + }, + "hrv": { + "default": "mdi:heart-flash" + }, + "respiratory_rate_avg": { + "default": "mdi:lungs" + }, + "sleep_duration": { + "default": "mdi:sleep" + }, + "sleep_score": { + "default": "mdi:sleep" + } + } + } +} diff --git a/homeassistant/components/sleepiq/sensor.py b/homeassistant/components/sleepiq/sensor.py index 0b8f7fc50023a..5d22897d97b31 100644 --- a/homeassistant/components/sleepiq/sensor.py +++ b/homeassistant/components/sleepiq/sensor.py @@ -8,16 +8,31 @@ from asyncsleepiq import SleepIQBed, SleepIQSleeper from homeassistant.components.sensor import ( + SensorDeviceClass, SensorEntity, SensorEntityDescription, SensorStateClass, ) from homeassistant.config_entries import ConfigEntry +from homeassistant.const import UnitOfTime from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN, PRESSURE, SLEEP_NUMBER -from .coordinator import SleepIQData, SleepIQDataUpdateCoordinator +from .const import ( + DOMAIN, + HEART_RATE, + HRV, + PRESSURE, + RESPIRATORY_RATE, + SLEEP_DURATION, + SLEEP_NUMBER, + SLEEP_SCORE, +) +from .coordinator import ( + SleepIQData, + SleepIQDataUpdateCoordinator, + SleepIQSleepDataCoordinator, +) from .entity import SleepIQSleeperEntity @@ -28,7 +43,7 @@ class SleepIQSensorEntityDescription(SensorEntityDescription): value_fn: Callable[[SleepIQSleeper], float | int | None] -SENSORS: tuple[SleepIQSensorEntityDescription, ...] = ( +BED_SENSORS: tuple[SleepIQSensorEntityDescription, ...] = ( SleepIQSensorEntityDescription( key=PRESSURE, translation_key="pressure", @@ -43,6 +58,57 @@ class SleepIQSensorEntityDescription(SensorEntityDescription): ), ) +SLEEP_HEALTH_SENSORS: tuple[SleepIQSensorEntityDescription, ...] = ( + SleepIQSensorEntityDescription( + key=SLEEP_SCORE, + translation_key="sleep_score", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement="score", + value_fn=lambda sleeper: ( + sleeper.sleep_data.sleep_score if sleeper.sleep_data else None + ), + ), + SleepIQSensorEntityDescription( + key=SLEEP_DURATION, + translation_key="sleep_duration", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTime.HOURS, + suggested_display_precision=1, + value_fn=lambda sleeper: ( + round(sleeper.sleep_data.duration / 3600, 1) + if sleeper.sleep_data and sleeper.sleep_data.duration + else None + ), + ), + SleepIQSensorEntityDescription( + key=HEART_RATE, + translation_key="heart_rate_avg", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement="bpm", + value_fn=lambda sleeper: ( + sleeper.sleep_data.heart_rate if sleeper.sleep_data else None + ), + ), + SleepIQSensorEntityDescription( + key=RESPIRATORY_RATE, + translation_key="respiratory_rate_avg", + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement="brpm", + value_fn=lambda sleeper: ( + sleeper.sleep_data.respiratory_rate if sleeper.sleep_data else None + ), + ), + SleepIQSensorEntityDescription( + key=HRV, + translation_key="hrv", + device_class=SensorDeviceClass.DURATION, + state_class=SensorStateClass.MEASUREMENT, + native_unit_of_measurement=UnitOfTime.MILLISECONDS, + value_fn=lambda sleeper: sleeper.sleep_data.hrv if sleeper.sleep_data else None, + ), +) + async def async_setup_entry( hass: HomeAssistant, @@ -51,16 +117,29 @@ async def async_setup_entry( ) -> None: """Set up the SleepIQ bed sensors.""" data: SleepIQData = hass.data[DOMAIN][entry.entry_id] - async_add_entities( + + entities: list[SensorEntity] = [] + + entities.extend( SleepIQSensorEntity(data.data_coordinator, bed, sleeper, description) for bed in data.client.beds.values() for sleeper in bed.sleepers - for description in SENSORS + for description in BED_SENSORS ) + entities.extend( + SleepIQSensorEntity(data.sleep_data_coordinator, bed, sleeper, description) + for bed in data.client.beds.values() + for sleeper in bed.sleepers + for description in SLEEP_HEALTH_SENSORS + ) + + async_add_entities(entities) + class SleepIQSensorEntity( - SleepIQSleeperEntity[SleepIQDataUpdateCoordinator], SensorEntity + SleepIQSleeperEntity[SleepIQDataUpdateCoordinator | SleepIQSleepDataCoordinator], + SensorEntity, ): """Representation of a SleepIQ sensor.""" @@ -68,7 +147,7 @@ class SleepIQSensorEntity( def __init__( self, - coordinator: SleepIQDataUpdateCoordinator, + coordinator: SleepIQDataUpdateCoordinator | SleepIQSleepDataCoordinator, bed: SleepIQBed, sleeper: SleepIQSleeper, description: SleepIQSensorEntityDescription, diff --git a/tests/components/sleepiq/conftest.py b/tests/components/sleepiq/conftest.py index f52f489aec387..683f6e3b20c63 100644 --- a/tests/components/sleepiq/conftest.py +++ b/tests/components/sleepiq/conftest.py @@ -10,6 +10,7 @@ CoreTemps, FootWarmingTemps, Side, + SleepData, SleepIQActuator, SleepIQBed, SleepIQCoreClimate, @@ -76,6 +77,13 @@ def mock_bed() -> MagicMock: sleeper_l.sleep_number = 40 sleeper_l.pressure = 1000 sleeper_l.sleeper_id = SLEEPER_L_ID + sleeper_l.sleep_data = SleepData( + duration=28800, # 8 hours in seconds + sleep_score=85, + heart_rate=60, + respiratory_rate=14, + hrv=68, + ) sleeper_r.side = Side.RIGHT sleeper_r.name = SLEEPER_R_NAME @@ -83,6 +91,13 @@ def mock_bed() -> MagicMock: sleeper_r.sleep_number = 80 sleeper_r.pressure = 1400 sleeper_r.sleeper_id = SLEEPER_R_ID + sleeper_r.sleep_data = SleepData( + duration=25200, # 7 hours in seconds + sleep_score=78, + heart_rate=65, + respiratory_rate=15, + hrv=72, + ) bed.foundation = create_autospec(SleepIQFoundation) light_1 = create_autospec(SleepIQLight) diff --git a/tests/components/sleepiq/snapshots/test_sensor.ambr b/tests/components/sleepiq/snapshots/test_sensor.ambr index 22093c0fb37f0..45c7ff08a8f62 100644 --- a/tests/components/sleepiq/snapshots/test_sensor.ambr +++ b/tests/components/sleepiq/snapshots/test_sensor.ambr @@ -1,4 +1,116 @@ # serializer version: 1 +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_heart_rate_average-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_heart_rate_average', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed Sleeper R Heart Rate Average', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed Sleeper R Heart Rate Average', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'heart_rate_avg', + 'unique_id': '43219_heart_rate', + 'unit_of_measurement': 'bpm', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_heart_rate_average-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed Sleeper R Heart Rate Average', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'bpm', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_heart_rate_average', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '65', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_heart_rate_variability-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_heart_rate_variability', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed Sleeper R Heart Rate Variability', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed Sleeper R Heart Rate Variability', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'hrv', + 'unique_id': '43219_hrv', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_heart_rate_variability-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': 'SleepNumber Test Bed Sleeper R Heart Rate Variability', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_heart_rate_variability', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '72', + }) +# --- # name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_pressure-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -52,6 +164,172 @@ 'state': '1400', }) # --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_respiratory_rate_average-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_respiratory_rate_average', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed Sleeper R Respiratory Rate Average', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed Sleeper R Respiratory Rate Average', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'respiratory_rate_avg', + 'unique_id': '43219_respiratory_rate', + 'unit_of_measurement': 'brpm', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_respiratory_rate_average-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed Sleeper R Respiratory Rate Average', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'brpm', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_respiratory_rate_average', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '15', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_sleep_duration-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_sleep_duration', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed Sleeper R Sleep Duration', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed Sleeper R Sleep Duration', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sleep_duration', + 'unique_id': '43219_sleep_duration', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_sleep_duration-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': 'SleepNumber Test Bed Sleeper R Sleep Duration', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_sleep_duration', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '7.0', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_sleep_score-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_sleep_score', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed Sleeper R Sleep Score', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed Sleeper R Sleep Score', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sleep_score', + 'unique_id': '43219_sleep_score', + 'unit_of_measurement': 'score', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_sleep_score-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed Sleeper R Sleep Score', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'score', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeper_r_sleep_score', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '78', + }) +# --- # name: test_sensors[sensor.sleepnumber_test_bed_sleeper_r_sleepnumber-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -105,6 +383,118 @@ 'state': '80', }) # --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_heart_rate_average-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_heart_rate_average', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed SleeperL Heart Rate Average', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed SleeperL Heart Rate Average', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'heart_rate_avg', + 'unique_id': '98765_heart_rate', + 'unit_of_measurement': 'bpm', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_heart_rate_average-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed SleeperL Heart Rate Average', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'bpm', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_heart_rate_average', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '60', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_heart_rate_variability-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_heart_rate_variability', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed SleeperL Heart Rate Variability', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 0, + }), + }), + 'original_device_class': , + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed SleeperL Heart Rate Variability', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'hrv', + 'unique_id': '98765_hrv', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_heart_rate_variability-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': 'SleepNumber Test Bed SleeperL Heart Rate Variability', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_heart_rate_variability', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '68', + }) +# --- # name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_pressure-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ @@ -158,6 +548,172 @@ 'state': '1000', }) # --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_respiratory_rate_average-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_respiratory_rate_average', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed SleeperL Respiratory Rate Average', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed SleeperL Respiratory Rate Average', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'respiratory_rate_avg', + 'unique_id': '98765_respiratory_rate', + 'unit_of_measurement': 'brpm', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_respiratory_rate_average-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed SleeperL Respiratory Rate Average', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'brpm', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_respiratory_rate_average', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '14', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_sleep_duration-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_sleep_duration', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed SleeperL Sleep Duration', + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed SleeperL Sleep Duration', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sleep_duration', + 'unique_id': '98765_sleep_duration', + 'unit_of_measurement': , + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_sleep_duration-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'duration', + 'friendly_name': 'SleepNumber Test Bed SleeperL Sleep Duration', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_sleep_duration', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '8.0', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_sleep_score-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_sleep_score', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'SleepNumber Test Bed SleeperL Sleep Score', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:bed', + 'original_name': 'SleepNumber Test Bed SleeperL Sleep Score', + 'platform': 'sleepiq', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'sleep_score', + 'unique_id': '98765_sleep_score', + 'unit_of_measurement': 'score', + }) +# --- +# name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_sleep_score-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SleepNumber Test Bed SleeperL Sleep Score', + 'icon': 'mdi:bed', + 'state_class': , + 'unit_of_measurement': 'score', + }), + 'context': , + 'entity_id': 'sensor.sleepnumber_test_bed_sleeperl_sleep_score', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '85', + }) +# --- # name: test_sensors[sensor.sleepnumber_test_bed_sleeperl_sleepnumber-entry] EntityRegistryEntrySnapshot({ 'aliases': set({