mirror of
https://github.com/home-assistant/core.git
synced 2025-05-05 06:29:16 +00:00
Add switch platform to iaqualink integration (#26545)
* Add switch platform to iaqualink component * Remove unnecessary call to constructor
This commit is contained in:
parent
7dfdec531c
commit
1a73e6b44e
@ -291,6 +291,7 @@ omit =
|
||||
homeassistant/components/iaqualink/climate.py
|
||||
homeassistant/components/iaqualink/light.py
|
||||
homeassistant/components/iaqualink/sensor.py
|
||||
homeassistant/components/iaqualink/switch.py
|
||||
homeassistant/components/icloud/device_tracker.py
|
||||
homeassistant/components/idteck_prox/*
|
||||
homeassistant/components/ifttt/*
|
||||
|
@ -12,12 +12,14 @@ from iaqualink import (
|
||||
AqualinkLoginException,
|
||||
AqualinkSensor,
|
||||
AqualinkThermostat,
|
||||
AqualinkToggle,
|
||||
)
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
@ -77,6 +79,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
||||
climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = []
|
||||
lights = hass.data[DOMAIN][LIGHT_DOMAIN] = []
|
||||
sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = []
|
||||
switches = hass.data[DOMAIN][SWITCH_DOMAIN] = []
|
||||
|
||||
session = async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True))
|
||||
aqualink = AqualinkClient(username, password, session)
|
||||
@ -102,6 +105,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
||||
lights += [dev]
|
||||
elif isinstance(dev, AqualinkSensor):
|
||||
sensors += [dev]
|
||||
elif isinstance(dev, AqualinkToggle):
|
||||
switches += [dev]
|
||||
|
||||
forward_setup = hass.config_entries.async_forward_entry_setup
|
||||
if climates:
|
||||
@ -113,6 +118,9 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
||||
if sensors:
|
||||
_LOGGER.debug("Got %s sensors: %s", len(sensors), sensors)
|
||||
hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN))
|
||||
if switches:
|
||||
_LOGGER.debug("Got %s switches: %s", len(switches), switches)
|
||||
hass.async_create_task(forward_setup(entry, SWITCH_DOMAIN))
|
||||
|
||||
async def _async_systems_update(now):
|
||||
"""Refresh internal state for all systems."""
|
||||
@ -136,6 +144,8 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
|
||||
tasks += [forward_unload(entry, LIGHT_DOMAIN)]
|
||||
if hass.data[DOMAIN][SENSOR_DOMAIN]:
|
||||
tasks += [forward_unload(entry, SENSOR_DOMAIN)]
|
||||
if hass.data[DOMAIN][SWITCH_DOMAIN]:
|
||||
tasks += [forward_unload(entry, SWITCH_DOMAIN)]
|
||||
|
||||
hass.data[DOMAIN].clear()
|
||||
|
||||
|
65
homeassistant/components/iaqualink/switch.py
Normal file
65
homeassistant/components/iaqualink/switch.py
Normal file
@ -0,0 +1,65 @@
|
||||
"""Support for Aqualink pool feature switches."""
|
||||
import logging
|
||||
|
||||
from iaqualink import AqualinkToggle
|
||||
|
||||
from homeassistant.components.switch import DOMAIN, SwitchDevice
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import AqualinkEntity, refresh_system
|
||||
from .const import DOMAIN as AQUALINK_DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistantType, config_entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up discovered switches."""
|
||||
devs = []
|
||||
for dev in hass.data[AQUALINK_DOMAIN][DOMAIN]:
|
||||
devs.append(HassAqualinkSwitch(dev))
|
||||
async_add_entities(devs, True)
|
||||
|
||||
|
||||
class HassAqualinkSwitch(SwitchDevice, AqualinkEntity):
|
||||
"""Representation of a switch."""
|
||||
|
||||
def __init__(self, dev: AqualinkToggle):
|
||||
"""Initialize the switch."""
|
||||
self.dev = dev
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the switch."""
|
||||
return self.dev.label
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Return an icon based on the switch type."""
|
||||
if self.name == "Cleaner":
|
||||
return "mdi:robot-vacuum"
|
||||
if self.name == "Waterfall" or self.name.endswith("Dscnt"):
|
||||
return "mdi:fountain"
|
||||
if self.name.endswith("Pump") or self.name.endswith("Blower"):
|
||||
return "mdi:fan"
|
||||
if self.name.endswith("Heater"):
|
||||
return "mdi:radiator"
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return whether the switch is on or not."""
|
||||
return self.dev.is_on
|
||||
|
||||
@refresh_system
|
||||
async def async_turn_on(self, **kwargs) -> None:
|
||||
"""Turn on the switch."""
|
||||
await self.dev.turn_on()
|
||||
|
||||
@refresh_system
|
||||
async def async_turn_off(self, **kwargs) -> None:
|
||||
"""Turn off the switch."""
|
||||
await self.dev.turn_off()
|
Loading…
x
Reference in New Issue
Block a user