From 5ee8e93b8af238b8cb59f8c80ad95059c879514b Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 22 Dec 2024 17:03:48 +0100 Subject: [PATCH 1/5] Add binary states for indoor unit --- homeassistant/components/weheat/__init__.py | 2 +- .../components/weheat/binary_sensor.py | 94 +++++++ homeassistant/components/weheat/icons.json | 17 ++ homeassistant/components/weheat/strings.json | 17 ++ tests/components/weheat/conftest.py | 5 + .../weheat/snapshots/test_binary_sensor.ambr | 231 ++++++++++++++++++ tests/components/weheat/test_binary_sensor.py | 52 ++++ 7 files changed, 417 insertions(+), 1 deletion(-) create mode 100644 homeassistant/components/weheat/binary_sensor.py create mode 100644 tests/components/weheat/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/weheat/test_binary_sensor.py diff --git a/homeassistant/components/weheat/__init__.py b/homeassistant/components/weheat/__init__.py index d924d6ceaab45e..a043a3a68455fe 100644 --- a/homeassistant/components/weheat/__init__.py +++ b/homeassistant/components/weheat/__init__.py @@ -17,7 +17,7 @@ from .const import API_URL, LOGGER from .coordinator import WeheatDataUpdateCoordinator -PLATFORMS: list[Platform] = [Platform.SENSOR] +PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] type WeheatConfigEntry = ConfigEntry[list[WeheatDataUpdateCoordinator]] diff --git a/homeassistant/components/weheat/binary_sensor.py b/homeassistant/components/weheat/binary_sensor.py new file mode 100644 index 00000000000000..d88b8d7db439e5 --- /dev/null +++ b/homeassistant/components/weheat/binary_sensor.py @@ -0,0 +1,94 @@ +"""Platform for binary sensor integration.""" + +from collections.abc import Callable +from dataclasses import dataclass + +from weheat.abstractions.heat_pump import HeatPump + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType + +from . import WeheatConfigEntry +from .coordinator import WeheatDataUpdateCoordinator +from .entity import WeheatEntity + + +@dataclass(frozen=True, kw_only=True) +class WeHeatBinarySensorEntityDescription(BinarySensorEntityDescription): + """Describes Weheat binary sensor entity.""" + + value_fn: Callable[[HeatPump], StateType] + + +BINARY_SENSORS = [ + WeHeatBinarySensorEntityDescription( + translation_key="indoor_unit_water_pump_state", + key="indoor_unit_water_pump_state", + value_fn=lambda status: status.indoor_unit_water_pump_state, + ), + WeHeatBinarySensorEntityDescription( + translation_key="indoor_unit_auxiliary_pump_state", + key="indoor_unit_auxiliary_pump_state", + value_fn=lambda status: status.indoor_unit_auxiliary_pump_state, + ), + WeHeatBinarySensorEntityDescription( + translation_key="indoor_unit_dhw_valve_or_pump_state", + key="indoor_unit_dhw_valve_or_pump_state", + value_fn=lambda status: status.indoor_unit_dhw_valve_or_pump_state, + ), + WeHeatBinarySensorEntityDescription( + translation_key="indoor_unit_gas_boiler_state", + key="indoor_unit_gas_boiler_state", + value_fn=lambda status: status.indoor_unit_gas_boiler_state, + ), + WeHeatBinarySensorEntityDescription( + translation_key="indoor_unit_electric_heater_state", + key="indoor_unit_electric_heater_state", + value_fn=lambda status: status.indoor_unit_electric_heater_state, + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + entry: WeheatConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up the sensors for weheat heat pump.""" + entities = [ + WeheatHeatPumpBinarySensor(coordinator, entity_description) + for entity_description in BINARY_SENSORS + for coordinator in entry.runtime_data + ] + + async_add_entities(entities) + + +class WeheatHeatPumpBinarySensor(WeheatEntity, BinarySensorEntity): + """Defines a Weheat heat pump binary sensor.""" + + coordinator: WeheatDataUpdateCoordinator + entity_description: WeHeatBinarySensorEntityDescription + + def __init__( + self, + coordinator: WeheatDataUpdateCoordinator, + entity_description: WeHeatBinarySensorEntityDescription, + ) -> None: + """Pass coordinator to CoordinatorEntity.""" + super().__init__(coordinator) + + self.entity_description = entity_description + + self._attr_unique_id = f"{coordinator.heatpump_id}_{entity_description.key}" + + @property + def is_on(self) -> bool | None: + """Return True if the binary sensor is on.""" + value = self.entity_description.value_fn(self.coordinator.data) + return bool(value) if value is not None else None diff --git a/homeassistant/components/weheat/icons.json b/homeassistant/components/weheat/icons.json index 6fdae84cfffa5d..a0b1541cd18201 100644 --- a/homeassistant/components/weheat/icons.json +++ b/homeassistant/components/weheat/icons.json @@ -1,5 +1,22 @@ { "entity": { + "binary_sensor": { + "indoor_unit_water_pump_state": { + "default": "mdi:pump" + }, + "indoor_unit_auxiliary_pump_state": { + "default": "mdi:pump" + }, + "indoor_unit_dhw_valve_or_pump_state": { + "default": "mdi:pump" + }, + "indoor_unit_gas_boiler_state": { + "default": "mdi:toggle-switch" + }, + "indoor_unit_electric_heater_state": { + "default": "mdi:heating-coil" + } + }, "sensor": { "power_output": { "default": "mdi:heat-wave" diff --git a/homeassistant/components/weheat/strings.json b/homeassistant/components/weheat/strings.json index 0733024cbed436..be475348be9395 100644 --- a/homeassistant/components/weheat/strings.json +++ b/homeassistant/components/weheat/strings.json @@ -32,6 +32,23 @@ } }, "entity": { + "binary_sensor": { + "indoor_unit_water_pump_state": { + "name": "Indoor unit water pump state" + }, + "indoor_unit_auxiliary_pump_state": { + "name": "Indoor unit auxilary water pump state" + }, + "indoor_unit_dhw_valve_or_pump_state": { + "name": "Indoor unit DHW valve or water pump state" + }, + "indoor_unit_gas_boiler_state": { + "name": "Indoor unit gas boiler heating allowed" + }, + "indoor_unit_electric_heater_state": { + "name": "Indoor unit electric heater state" + } + }, "sensor": { "power_output": { "name": "Output power" diff --git a/tests/components/weheat/conftest.py b/tests/components/weheat/conftest.py index 6ecb64ffdf4d30..c1884f63258b7b 100644 --- a/tests/components/weheat/conftest.py +++ b/tests/components/weheat/conftest.py @@ -121,6 +121,11 @@ def mock_weheat_heat_pump_instance() -> MagicMock: mock_heat_pump_instance.cop = 4.5 mock_heat_pump_instance.heat_pump_state = HeatPump.State.HEATING mock_heat_pump_instance.energy_total = 12345 + mock_heat_pump_instance.indoor_unit_water_pump_state = False + mock_heat_pump_instance.indoor_unit_auxiliary_pump_state = False + mock_heat_pump_instance.indoor_unit_dhw_valve_or_pump_state = False + mock_heat_pump_instance.indoor_unit_gas_boiler_state = None + mock_heat_pump_instance.indoor_unit_electric_heater_state = True return mock_heat_pump_instance diff --git a/tests/components/weheat/snapshots/test_binary_sensor.ambr b/tests/components/weheat/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000000..4a4a724d0c3d09 --- /dev/null +++ b/tests/components/weheat/snapshots/test_binary_sensor.ambr @@ -0,0 +1,231 @@ +# serializer version: 1 +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump_state-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump_state', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Indoor unit auxilary water pump state', + 'platform': 'weheat', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'indoor_unit_auxiliary_pump_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_auxiliary_pump_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump_state-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Model Indoor unit auxilary water pump state', + }), + 'context': , + 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump_state', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Indoor unit DHW valve or water pump state', + 'platform': 'weheat', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'indoor_unit_dhw_valve_or_pump_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_dhw_valve_or_pump_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Model Indoor unit DHW valve or water pump state', + }), + 'context': , + 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater_state-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater_state', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Indoor unit electric heater state', + 'platform': 'weheat', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'indoor_unit_electric_heater_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_electric_heater_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater_state-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Model Indoor unit electric heater state', + }), + 'context': , + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater_state', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Indoor unit gas boiler heating allowed', + 'platform': 'weheat', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'indoor_unit_gas_boiler_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_gas_boiler_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Model Indoor unit gas boiler heating allowed', + }), + 'context': , + 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump_state-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump_state', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Indoor unit water pump state', + 'platform': 'weheat', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'indoor_unit_water_pump_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_water_pump_state', + 'unit_of_measurement': None, + }) +# --- +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump_state-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Test Model Indoor unit water pump state', + }), + 'context': , + 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump_state', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- diff --git a/tests/components/weheat/test_binary_sensor.py b/tests/components/weheat/test_binary_sensor.py new file mode 100644 index 00000000000000..5df465782f7a2b --- /dev/null +++ b/tests/components/weheat/test_binary_sensor.py @@ -0,0 +1,52 @@ +"""Tests for the weheat sensor platform.""" + +from unittest.mock import AsyncMock, patch + +from freezegun.api import FrozenDateTimeFactory +import pytest +from syrupy import SnapshotAssertion +from weheat.abstractions.discovery import HeatPumpDiscovery + +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import setup_integration + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +async def test_binary_entities( + hass: HomeAssistant, + snapshot: SnapshotAssertion, + mock_weheat_discover: AsyncMock, + mock_weheat_heat_pump: AsyncMock, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test all entities.""" + with patch("homeassistant.components.weheat.PLATFORMS", [Platform.BINARY_SENSOR]): + await setup_integration(hass, mock_config_entry) + + await hass.async_block_till_done() + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) + + +async def test_create_binary_entities( + hass: HomeAssistant, + mock_weheat_discover: AsyncMock, + mock_weheat_heat_pump: AsyncMock, + mock_heat_pump_info: HeatPumpDiscovery.HeatPumpInfo, + mock_config_entry: MockConfigEntry, + freezer: FrozenDateTimeFactory, +) -> None: + """Test creating entities.""" + mock_weheat_discover.return_value = [mock_heat_pump_info] + + with patch("homeassistant.components.weheat.PLATFORMS", [Platform.BINARY_SENSOR]): + await setup_integration(hass, mock_config_entry) + + await hass.async_block_till_done() + assert len(hass.states.async_all()) == 5 From 64552c0327dd37cd2853e2be1820acce22592112 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 22 Dec 2024 17:46:55 +0100 Subject: [PATCH 2/5] Update homeassistant/components/weheat/binary_sensor.py Co-authored-by: Joost Lekkerkerker --- homeassistant/components/weheat/binary_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/weheat/binary_sensor.py b/homeassistant/components/weheat/binary_sensor.py index d88b8d7db439e5..f59540ae728568 100644 --- a/homeassistant/components/weheat/binary_sensor.py +++ b/homeassistant/components/weheat/binary_sensor.py @@ -1,4 +1,4 @@ -"""Platform for binary sensor integration.""" +"""Binary sensor platform for Weheat integration.""" from collections.abc import Callable from dataclasses import dataclass From 027c1ed65e4dae70aa654ab99555d02e6c13c157 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 22 Dec 2024 18:29:44 +0100 Subject: [PATCH 3/5] Remove state from name, add running device class --- .../components/weheat/binary_sensor.py | 5 ++ homeassistant/components/weheat/strings.json | 8 +-- .../weheat/snapshots/test_binary_sensor.ambr | 60 ++++++++++--------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/homeassistant/components/weheat/binary_sensor.py b/homeassistant/components/weheat/binary_sensor.py index d88b8d7db439e5..1e520b4264413c 100644 --- a/homeassistant/components/weheat/binary_sensor.py +++ b/homeassistant/components/weheat/binary_sensor.py @@ -6,6 +6,7 @@ from weheat.abstractions.heat_pump import HeatPump from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, BinarySensorEntity, BinarySensorEntityDescription, ) @@ -29,16 +30,19 @@ class WeHeatBinarySensorEntityDescription(BinarySensorEntityDescription): WeHeatBinarySensorEntityDescription( translation_key="indoor_unit_water_pump_state", key="indoor_unit_water_pump_state", + device_class=BinarySensorDeviceClass.RUNNING, value_fn=lambda status: status.indoor_unit_water_pump_state, ), WeHeatBinarySensorEntityDescription( translation_key="indoor_unit_auxiliary_pump_state", key="indoor_unit_auxiliary_pump_state", + device_class=BinarySensorDeviceClass.RUNNING, value_fn=lambda status: status.indoor_unit_auxiliary_pump_state, ), WeHeatBinarySensorEntityDescription( translation_key="indoor_unit_dhw_valve_or_pump_state", key="indoor_unit_dhw_valve_or_pump_state", + device_class=BinarySensorDeviceClass.RUNNING, value_fn=lambda status: status.indoor_unit_dhw_valve_or_pump_state, ), WeHeatBinarySensorEntityDescription( @@ -49,6 +53,7 @@ class WeHeatBinarySensorEntityDescription(BinarySensorEntityDescription): WeHeatBinarySensorEntityDescription( translation_key="indoor_unit_electric_heater_state", key="indoor_unit_electric_heater_state", + device_class=BinarySensorDeviceClass.RUNNING, value_fn=lambda status: status.indoor_unit_electric_heater_state, ), ] diff --git a/homeassistant/components/weheat/strings.json b/homeassistant/components/weheat/strings.json index be475348be9395..fdaea1b9bf6e7d 100644 --- a/homeassistant/components/weheat/strings.json +++ b/homeassistant/components/weheat/strings.json @@ -34,19 +34,19 @@ "entity": { "binary_sensor": { "indoor_unit_water_pump_state": { - "name": "Indoor unit water pump state" + "name": "Indoor unit water pump" }, "indoor_unit_auxiliary_pump_state": { - "name": "Indoor unit auxilary water pump state" + "name": "Indoor unit auxilary water pump" }, "indoor_unit_dhw_valve_or_pump_state": { - "name": "Indoor unit DHW valve or water pump state" + "name": "Indoor unit DHW valve or water pump" }, "indoor_unit_gas_boiler_state": { "name": "Indoor unit gas boiler heating allowed" }, "indoor_unit_electric_heater_state": { - "name": "Indoor unit electric heater state" + "name": "Indoor unit electric heater" } }, "sensor": { diff --git a/tests/components/weheat/snapshots/test_binary_sensor.ambr b/tests/components/weheat/snapshots/test_binary_sensor.ambr index 4a4a724d0c3d09..22226c2abd429c 100644 --- a/tests/components/weheat/snapshots/test_binary_sensor.ambr +++ b/tests/components/weheat/snapshots/test_binary_sensor.ambr @@ -1,5 +1,5 @@ # serializer version: 1 -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump_state-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -11,7 +11,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -21,9 +21,9 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , 'original_icon': None, - 'original_name': 'Indoor unit auxilary water pump state', + 'original_name': 'Indoor unit auxilary water pump', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, @@ -32,20 +32,21 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump_state-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Test Model Indoor unit auxilary water pump state', + 'device_class': 'running', + 'friendly_name': 'Test Model Indoor unit auxilary water pump', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump', 'last_changed': , 'last_reported': , 'last_updated': , 'state': 'off', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -57,7 +58,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -67,9 +68,9 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , 'original_icon': None, - 'original_name': 'Indoor unit DHW valve or water pump state', + 'original_name': 'Indoor unit DHW valve or water pump', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, @@ -78,20 +79,21 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Test Model Indoor unit DHW valve or water pump state', + 'device_class': 'running', + 'friendly_name': 'Test Model Indoor unit DHW valve or water pump', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump', 'last_changed': , 'last_reported': , 'last_updated': , 'state': 'off', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater_state-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -103,7 +105,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -113,9 +115,9 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , 'original_icon': None, - 'original_name': 'Indoor unit electric heater state', + 'original_name': 'Indoor unit electric heater', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, @@ -124,13 +126,14 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater_state-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Test Model Indoor unit electric heater state', + 'device_class': 'running', + 'friendly_name': 'Test Model Indoor unit electric heater', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', 'last_changed': , 'last_reported': , 'last_updated': , @@ -183,7 +186,7 @@ 'state': 'unknown', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump_state-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -195,7 +198,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -205,9 +208,9 @@ 'name': None, 'options': dict({ }), - 'original_device_class': None, + 'original_device_class': , 'original_icon': None, - 'original_name': 'Indoor unit water pump state', + 'original_name': 'Indoor unit water pump', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, @@ -216,13 +219,14 @@ 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump_state-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Test Model Indoor unit water pump state', + 'device_class': 'running', + 'friendly_name': 'Test Model Indoor unit water pump', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump_state', + 'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump', 'last_changed': , 'last_reported': , 'last_updated': , From c2361ebcdd40379c9633573e2b57ccbff9963860 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 22 Dec 2024 18:59:00 +0100 Subject: [PATCH 4/5] Only set sensors when available --- .../components/weheat/binary_sensor.py | 1 + .../weheat/snapshots/test_binary_sensor.ambr | 46 ------------------- tests/components/weheat/test_binary_sensor.py | 2 +- 3 files changed, 2 insertions(+), 47 deletions(-) diff --git a/homeassistant/components/weheat/binary_sensor.py b/homeassistant/components/weheat/binary_sensor.py index 2f577f5e667cf5..ea939227e77e40 100644 --- a/homeassistant/components/weheat/binary_sensor.py +++ b/homeassistant/components/weheat/binary_sensor.py @@ -69,6 +69,7 @@ async def async_setup_entry( WeheatHeatPumpBinarySensor(coordinator, entity_description) for entity_description in BINARY_SENSORS for coordinator in entry.runtime_data + if entity_description.value_fn(coordinator.data) is not None ] async_add_entities(entities) diff --git a/tests/components/weheat/snapshots/test_binary_sensor.ambr b/tests/components/weheat/snapshots/test_binary_sensor.ambr index 22226c2abd429c..2be45e28469670 100644 --- a/tests/components/weheat/snapshots/test_binary_sensor.ambr +++ b/tests/components/weheat/snapshots/test_binary_sensor.ambr @@ -140,52 +140,6 @@ 'state': 'on', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-entry] - EntityRegistryEntrySnapshot({ - 'aliases': set({ - }), - 'area_id': None, - 'capabilities': None, - 'config_entry_id': , - 'device_class': None, - 'device_id': , - 'disabled_by': None, - 'domain': 'binary_sensor', - 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', - 'has_entity_name': True, - 'hidden_by': None, - 'icon': None, - 'id': , - 'labels': set({ - }), - 'name': None, - 'options': dict({ - }), - 'original_device_class': None, - 'original_icon': None, - 'original_name': 'Indoor unit gas boiler heating allowed', - 'platform': 'weheat', - 'previous_unique_id': None, - 'supported_features': 0, - 'translation_key': 'indoor_unit_gas_boiler_state', - 'unique_id': '0000-1111-2222-3333_indoor_unit_gas_boiler_state', - 'unit_of_measurement': None, - }) -# --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-state] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'Test Model Indoor unit gas boiler heating allowed', - }), - 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', - 'last_changed': , - 'last_reported': , - 'last_updated': , - 'state': 'unknown', - }) -# --- # name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/weheat/test_binary_sensor.py b/tests/components/weheat/test_binary_sensor.py index 5df465782f7a2b..e75cb282e2460e 100644 --- a/tests/components/weheat/test_binary_sensor.py +++ b/tests/components/weheat/test_binary_sensor.py @@ -49,4 +49,4 @@ async def test_create_binary_entities( await setup_integration(hass, mock_config_entry) await hass.async_block_till_done() - assert len(hass.states.async_all()) == 5 + assert len(hass.states.async_all()) == 4 From e51917920011754aa6894983beed94d4734de3af Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sun, 22 Dec 2024 19:01:24 +0100 Subject: [PATCH 5/5] Tweak mock --- tests/components/weheat/conftest.py | 4 +- .../weheat/snapshots/test_binary_sensor.ambr | 39 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/tests/components/weheat/conftest.py b/tests/components/weheat/conftest.py index c1884f63258b7b..b6a62194227fb2 100644 --- a/tests/components/weheat/conftest.py +++ b/tests/components/weheat/conftest.py @@ -123,8 +123,8 @@ def mock_weheat_heat_pump_instance() -> MagicMock: mock_heat_pump_instance.energy_total = 12345 mock_heat_pump_instance.indoor_unit_water_pump_state = False mock_heat_pump_instance.indoor_unit_auxiliary_pump_state = False - mock_heat_pump_instance.indoor_unit_dhw_valve_or_pump_state = False - mock_heat_pump_instance.indoor_unit_gas_boiler_state = None + mock_heat_pump_instance.indoor_unit_dhw_valve_or_pump_state = None + mock_heat_pump_instance.indoor_unit_gas_boiler_state = False mock_heat_pump_instance.indoor_unit_electric_heater_state = True return mock_heat_pump_instance diff --git a/tests/components/weheat/snapshots/test_binary_sensor.ambr b/tests/components/weheat/snapshots/test_binary_sensor.ambr index 2be45e28469670..08d609ca610c60 100644 --- a/tests/components/weheat/snapshots/test_binary_sensor.ambr +++ b/tests/components/weheat/snapshots/test_binary_sensor.ambr @@ -46,7 +46,7 @@ 'state': 'off', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -58,7 +58,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump', + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -70,30 +70,30 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Indoor unit DHW valve or water pump', + 'original_name': 'Indoor unit electric heater', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': 'indoor_unit_dhw_valve_or_pump_state', - 'unique_id': '0000-1111-2222-3333_indoor_unit_dhw_valve_or_pump_state', + 'translation_key': 'indoor_unit_electric_heater_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_electric_heater_state', 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'running', - 'friendly_name': 'Test Model Indoor unit DHW valve or water pump', + 'friendly_name': 'Test Model Indoor unit electric heater', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_dhw_valve_or_water_pump', + 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'off', + 'state': 'on', }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-entry] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -105,7 +105,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', + 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -115,29 +115,28 @@ 'name': None, 'options': dict({ }), - 'original_device_class': , + 'original_device_class': None, 'original_icon': None, - 'original_name': 'Indoor unit electric heater', + 'original_name': 'Indoor unit gas boiler heating allowed', 'platform': 'weheat', 'previous_unique_id': None, 'supported_features': 0, - 'translation_key': 'indoor_unit_electric_heater_state', - 'unique_id': '0000-1111-2222-3333_indoor_unit_electric_heater_state', + 'translation_key': 'indoor_unit_gas_boiler_state', + 'unique_id': '0000-1111-2222-3333_indoor_unit_gas_boiler_state', 'unit_of_measurement': None, }) # --- -# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-state] +# name: test_binary_entities[binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'device_class': 'running', - 'friendly_name': 'Test Model Indoor unit electric heater', + 'friendly_name': 'Test Model Indoor unit gas boiler heating allowed', }), 'context': , - 'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater', + 'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed', 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'on', + 'state': 'off', }) # --- # name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump-entry]