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 4 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
99 changes: 99 additions & 0 deletions homeassistant/components/weheat/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""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
]

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 @@ -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

Expand Down
Loading