Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary states for Weheat indoor unit #133811

Merged
merged 7 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion homeassistant/components/weheat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]

Expand Down
100 changes: 100 additions & 0 deletions homeassistant/components/weheat/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
"""Binary sensor platform for Weheat integration."""

from collections.abc import Callable
from dataclasses import dataclass

from weheat.abstractions.heat_pump import HeatPump

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
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",
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(
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",
device_class=BinarySensorDeviceClass.RUNNING,
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
if entity_description.value_fn(coordinator.data) is not None
]

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
17 changes: 17 additions & 0 deletions homeassistant/components/weheat/icons.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
17 changes: 17 additions & 0 deletions homeassistant/components/weheat/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@
}
},
"entity": {
"binary_sensor": {
"indoor_unit_water_pump_state": {
"name": "Indoor unit water pump"
},
"indoor_unit_auxiliary_pump_state": {
"name": "Indoor unit auxilary water pump"
},
"indoor_unit_dhw_valve_or_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"
}
},
"sensor": {
"power_output": {
"name": "Output power"
Expand Down
5 changes: 5 additions & 0 deletions tests/components/weheat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def mock_weheat_heat_pump_instance() -> MagicMock:
mock_heat_pump_instance.energy_output = 56789
mock_heat_pump_instance.compressor_rpm = 4500
mock_heat_pump_instance.compressor_percentage = 100
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 = 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

Expand Down
188 changes: 188 additions & 0 deletions tests/components/weheat/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# serializer version: 1
# name: test_binary_entities[binary_sensor.test_model_indoor_unit_auxilary_water_pump-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.RUNNING: 'running'>,
'original_icon': None,
'original_name': 'Indoor unit auxilary water pump',
'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]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'running',
'friendly_name': 'Test Model Indoor unit auxilary water pump',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.test_model_indoor_unit_auxilary_water_pump',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_entities[binary_sensor.test_model_indoor_unit_electric_heater-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.RUNNING: 'running'>,
'original_icon': None,
'original_name': 'Indoor unit electric heater',
'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]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'running',
'friendly_name': 'Test Model Indoor unit electric heater',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.test_model_indoor_unit_electric_heater',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'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': <ANY>,
'device_class': None,
'device_id': <ANY>,
'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': <ANY>,
'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': <ANY>,
'entity_id': 'binary_sensor.test_model_indoor_unit_gas_boiler_heating_allowed',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_entities[binary_sensor.test_model_indoor_unit_water_pump-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': None,
'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <BinarySensorDeviceClass.RUNNING: 'running'>,
'original_icon': None,
'original_name': 'Indoor unit water pump',
'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]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'running',
'friendly_name': 'Test Model Indoor unit water pump',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.test_model_indoor_unit_water_pump',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
Loading