diff --git a/.coveragerc b/.coveragerc index cb42ac67231..98992bed247 100644 --- a/.coveragerc +++ b/.coveragerc @@ -838,10 +838,11 @@ omit = homeassistant/components/nfandroidtv/__init__.py homeassistant/components/nfandroidtv/notify.py homeassistant/components/nibe_heatpump/__init__.py + homeassistant/components/nibe_heatpump/binary_sensor.py homeassistant/components/nibe_heatpump/number.py homeassistant/components/nibe_heatpump/select.py homeassistant/components/nibe_heatpump/sensor.py - homeassistant/components/nibe_heatpump/binary_sensor.py + homeassistant/components/nibe_heatpump/switch.py homeassistant/components/niko_home_control/light.py homeassistant/components/nilu/air_quality.py homeassistant/components/nissan_leaf/* diff --git a/homeassistant/components/nibe_heatpump/__init__.py b/homeassistant/components/nibe_heatpump/__init__.py index 35c43f85543..590afba5b79 100644 --- a/homeassistant/components/nibe_heatpump/__init__.py +++ b/homeassistant/components/nibe_heatpump/__init__.py @@ -44,6 +44,7 @@ PLATFORMS: list[Platform] = [ Platform.NUMBER, Platform.SELECT, Platform.SENSOR, + Platform.SWITCH, ] COIL_READ_RETRIES = 5 diff --git a/homeassistant/components/nibe_heatpump/switch.py b/homeassistant/components/nibe_heatpump/switch.py new file mode 100644 index 00000000000..133e3d7244c --- /dev/null +++ b/homeassistant/components/nibe_heatpump/switch.py @@ -0,0 +1,52 @@ +"""The Nibe Heat Pump switch.""" +from __future__ import annotations + +from typing import Any + +from nibe.coil import Coil + +from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity import EntityCategory +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from . import DOMAIN, CoilEntity, Coordinator + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + """Set up platform.""" + + coordinator: Coordinator = hass.data[DOMAIN][config_entry.entry_id] + + async_add_entities( + Switch(coordinator, coil) + for coil in coordinator.coils + if coil.is_writable and coil.is_boolean + ) + + +class Switch(CoilEntity, SwitchEntity): + """Switch entity.""" + + _attr_entity_category = EntityCategory.CONFIG + + def __init__(self, coordinator: Coordinator, coil: Coil) -> None: + """Initialize entity.""" + super().__init__(coordinator, coil, ENTITY_ID_FORMAT) + self._attr_is_on = None + + def _async_read_coil(self, coil: Coil) -> None: + self._attr_is_on = coil.value == "ON" + + async def async_turn_on(self, **kwargs: Any) -> None: + """Turn the entity on.""" + await self._async_write_coil("ON") + + async def async_turn_off(self, **kwargs: Any) -> None: + """Turn the entity off.""" + await self._async_write_coil("OFF")