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 toggle switch to Daikin HVAC units #95954

Merged
merged 9 commits into from Nov 24, 2023
35 changes: 34 additions & 1 deletion homeassistant/components/daikin/switch.py
Expand Up @@ -13,8 +13,10 @@

ZONE_ICON = "mdi:home-circle"
STREAMER_ICON = "mdi:air-filter"
TOGGLE_ICON = "mdi:power"
DAIKIN_ATTR_ADVANCED = "adv"
DAIKIN_ATTR_STREAMER = "streamer"
DAIKIN_ATTR_MODE = "mode"


async def async_setup_platform(
Expand All @@ -35,7 +37,7 @@ async def async_setup_entry(
) -> None:
"""Set up Daikin climate based on config_entry."""
daikin_api: DaikinApi = hass.data[DAIKIN_DOMAIN][entry.entry_id]
switches: list[DaikinZoneSwitch | DaikinStreamerSwitch] = []
switches: list[DaikinZoneSwitch | DaikinStreamerSwitch | DaikinToggleSwitch] = []
if zones := daikin_api.device.zones:
switches.extend(
[
Expand All @@ -49,6 +51,7 @@ async def async_setup_entry(
# device supports the streamer, so assume so if it does support
# advanced modes.
switches.append(DaikinStreamerSwitch(daikin_api))
switches.append(DaikinToggleSwitch(daikin_api))
async_add_entities(switches)


Expand Down Expand Up @@ -119,3 +122,33 @@ async def async_turn_on(self, **kwargs: Any) -> None:
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the zone off."""
await self._api.device.set_streamer("off")


class DaikinToggleSwitch(SwitchEntity):
"""Switch state."""

_attr_icon = TOGGLE_ICON
_attr_has_entity_name = True

def __init__(self, api: DaikinApi) -> None:
"""Initialize switch."""
self._api = api
self._attr_device_info = api.device_info
self._attr_unique_id = f"{self._api.device.mac}-toggle"

@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return "off" not in self._api.device.represent(DAIKIN_ATTR_MODE)

async def async_update(self) -> None:
"""Retrieve latest state."""
await self._api.async_update()

async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the zone on."""
await self._api.device.set({})

async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the zone off."""
await self._api.device.set({DAIKIN_ATTR_MODE: "off"})