Skip to content

Commit

Permalink
Add binary_platform to iaqualink integration, add unique_id
Browse files Browse the repository at this point in the history
  • Loading branch information
flz committed Sep 13, 2019
1 parent 32a6a76 commit d148711
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ omit =
homeassistant/components/hydrawise/*
homeassistant/components/hyperion/light.py
homeassistant/components/ialarm/alarm_control_panel.py
homeassistant/components/iaqualink/binary_sensor.py
homeassistant/components/iaqualink/climate.py
homeassistant/components/iaqualink/light.py
homeassistant/components/iaqualink/sensor.py
Expand Down
18 changes: 16 additions & 2 deletions homeassistant/components/iaqualink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import voluptuous as vol

from iaqualink import (
AqualinkBinarySensor,
AqualinkClient,
AqualinkLight,
AqualinkLoginException,
Expand All @@ -16,6 +17,7 @@
)

from homeassistant import config_entries
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
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
Expand All @@ -28,7 +30,6 @@
async_dispatcher_connect,
async_dispatcher_send,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_time_interval
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
Expand Down Expand Up @@ -76,6 +77,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
password = entry.data[CONF_PASSWORD]

# These will contain the initialized devices
binary_sensors = hass.data[DOMAIN][BINARY_SENSOR_DOMAIN] = []
climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = []
lights = hass.data[DOMAIN][LIGHT_DOMAIN] = []
sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = []
Expand Down Expand Up @@ -103,12 +105,17 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
climates += [dev]
elif isinstance(dev, AqualinkLight):
lights += [dev]
elif isinstance(dev, AqualinkBinarySensor):
binary_sensors += [dev]
elif isinstance(dev, AqualinkSensor):
sensors += [dev]
elif isinstance(dev, AqualinkToggle):
switches += [dev]

forward_setup = hass.config_entries.async_forward_entry_setup
if binary_sensors:
_LOGGER.debug("Got %s binary sensors: %s", len(binary_sensors), binary_sensors)
hass.async_create_task(forward_setup(entry, BINARY_SENSOR_DOMAIN))
if climates:
_LOGGER.debug("Got %s climates: %s", len(climates), climates)
hass.async_create_task(forward_setup(entry, CLIMATE_DOMAIN))
Expand Down Expand Up @@ -138,6 +145,8 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo

tasks = []

if hass.data[DOMAIN][BINARY_SENSOR_DOMAIN]:
tasks += [forward_unload(entry, BINARY_SENSOR_DOMAIN)]
if hass.data[DOMAIN][CLIMATE_DOMAIN]:
tasks += [forward_unload(entry, CLIMATE_DOMAIN)]
if hass.data[DOMAIN][LIGHT_DOMAIN]:
Expand All @@ -164,7 +173,7 @@ async def wrapper(self, *args, **kwargs):
return wrapper


class AqualinkEntity(Entity):
class AqualinkEntityMixin:
"""Abstract class for all Aqualink platforms.
Entity state is updated via the interval timer within the integration.
Expand All @@ -190,3 +199,8 @@ def should_poll(self) -> bool:
updates on a timer.
"""
return False

@property
def unique_id(self) -> str:
"""Return a unique identifier for this entity."""
return "iaqualink_{0}_{1}".format(self.dev.system.serial, self.dev.name)
54 changes: 54 additions & 0 deletions homeassistant/components/iaqualink/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Support for Aqualink temperature sensors."""
import logging

from iaqualink import AqualinkBinarySensor

from homeassistant.components.binary_sensor import (
BinarySensorDevice,
DEVICE_CLASS_COLD,
DOMAIN,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType

from . import AqualinkEntityMixin
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 binary sensors."""
devs = []
for dev in hass.data[AQUALINK_DOMAIN][DOMAIN]:
devs.append(HassAqualinkBinarySensor(dev))
async_add_entities(devs, True)


class HassAqualinkBinarySensor(BinarySensorDevice, AqualinkEntityMixin):
"""Representation of a binary sensor."""

def __init__(self, dev: AqualinkBinarySensor):
"""Initialize the binary sensor."""
self.dev = dev

@property
def name(self) -> str:
"""Return the name of the binary sensor."""
return self.dev.label

@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on or not."""
return self.dev.is_on

@property
def device_class(self) -> str:
"""Return the class of the binary sensor."""
if self.name == "Freeze Protection":
return DEVICE_CLASS_COLD
return None
4 changes: 2 additions & 2 deletions homeassistant/components/iaqualink/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.helpers.typing import HomeAssistantType

from . import AqualinkEntity, refresh_system
from . import AqualinkEntityMixin, refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN, CLIMATE_SUPPORTED_MODES

_LOGGER = logging.getLogger(__name__)
Expand All @@ -45,7 +45,7 @@ async def async_setup_entry(
async_add_entities(devs, True)


class HassAqualinkThermostat(ClimateDevice, AqualinkEntity):
class HassAqualinkThermostat(ClimateDevice, AqualinkEntityMixin):
"""Representation of a thermostat."""

def __init__(self, dev: AqualinkThermostat):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/iaqualink/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType

from . import AqualinkEntity, refresh_system
from . import AqualinkEntityMixin, refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN

_LOGGER = logging.getLogger(__name__)
Expand All @@ -32,7 +32,7 @@ async def async_setup_entry(
async_add_entities(devs, True)


class HassAqualinkLight(Light, AqualinkEntity):
class HassAqualinkLight(Light, AqualinkEntityMixin):
"""Representation of a light."""

def __init__(self, dev: AqualinkLight):
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/iaqualink/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from homeassistant.components.sensor import DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.typing import HomeAssistantType

from . import AqualinkEntity
from . import AqualinkEntityMixin
from .const import DOMAIN as AQUALINK_DOMAIN

_LOGGER = logging.getLogger(__name__)
Expand All @@ -27,7 +28,7 @@ async def async_setup_entry(
async_add_entities(devs, True)


class HassAqualinkSensor(AqualinkEntity):
class HassAqualinkSensor(Entity, AqualinkEntityMixin):
"""Representation of a sensor."""

def __init__(self, dev: AqualinkSensor):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/iaqualink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.typing import HomeAssistantType

from . import AqualinkEntity, refresh_system
from . import AqualinkEntityMixin, refresh_system
from .const import DOMAIN as AQUALINK_DOMAIN

_LOGGER = logging.getLogger(__name__)
Expand All @@ -25,7 +25,7 @@ async def async_setup_entry(
async_add_entities(devs, True)


class HassAqualinkSwitch(SwitchDevice, AqualinkEntity):
class HassAqualinkSwitch(SwitchDevice, AqualinkEntityMixin):
"""Representation of a switch."""

def __init__(self, dev: AqualinkToggle):
Expand Down

0 comments on commit d148711

Please sign in to comment.