Add sensor platform to iaqualink component (#26544)
* Add sensor platform to iaqualink component * Remove unnecessary icon, fix case where value is 0
This commit is contained in:
parent
fbc3376c32
commit
a7830bc2d2
3 changed files with 70 additions and 0 deletions
|
@ -290,6 +290,7 @@ omit =
|
|||
homeassistant/components/ialarm/alarm_control_panel.py
|
||||
homeassistant/components/iaqualink/climate.py
|
||||
homeassistant/components/iaqualink/light.py
|
||||
homeassistant/components/iaqualink/sensor.py
|
||||
homeassistant/components/icloud/device_tracker.py
|
||||
homeassistant/components/idteck_prox/*
|
||||
homeassistant/components/ifttt/*
|
||||
|
|
|
@ -10,12 +10,14 @@ from iaqualink import (
|
|||
AqualinkClient,
|
||||
AqualinkLight,
|
||||
AqualinkLoginException,
|
||||
AqualinkSensor,
|
||||
AqualinkThermostat,
|
||||
)
|
||||
|
||||
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.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
|
@ -74,6 +76,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
|||
# These will contain the initialized devices
|
||||
climates = hass.data[DOMAIN][CLIMATE_DOMAIN] = []
|
||||
lights = hass.data[DOMAIN][LIGHT_DOMAIN] = []
|
||||
sensors = hass.data[DOMAIN][SENSOR_DOMAIN] = []
|
||||
|
||||
session = async_create_clientsession(hass, cookie_jar=CookieJar(unsafe=True))
|
||||
aqualink = AqualinkClient(username, password, session)
|
||||
|
@ -97,6 +100,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
|||
climates += [dev]
|
||||
elif isinstance(dev, AqualinkLight):
|
||||
lights += [dev]
|
||||
elif isinstance(dev, AqualinkSensor):
|
||||
sensors += [dev]
|
||||
|
||||
forward_setup = hass.config_entries.async_forward_entry_setup
|
||||
if climates:
|
||||
|
@ -105,6 +110,9 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> None
|
|||
if lights:
|
||||
_LOGGER.debug("Got %s lights: %s", len(lights), lights)
|
||||
hass.async_create_task(forward_setup(entry, LIGHT_DOMAIN))
|
||||
if sensors:
|
||||
_LOGGER.debug("Got %s sensors: %s", len(sensors), sensors)
|
||||
hass.async_create_task(forward_setup(entry, SENSOR_DOMAIN))
|
||||
|
||||
async def _async_systems_update(now):
|
||||
"""Refresh internal state for all systems."""
|
||||
|
@ -126,6 +134,8 @@ async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> boo
|
|||
tasks += [forward_unload(entry, CLIMATE_DOMAIN)]
|
||||
if hass.data[DOMAIN][LIGHT_DOMAIN]:
|
||||
tasks += [forward_unload(entry, LIGHT_DOMAIN)]
|
||||
if hass.data[DOMAIN][SENSOR_DOMAIN]:
|
||||
tasks += [forward_unload(entry, SENSOR_DOMAIN)]
|
||||
|
||||
hass.data[DOMAIN].clear()
|
||||
|
||||
|
|
59
homeassistant/components/iaqualink/sensor.py
Normal file
59
homeassistant/components/iaqualink/sensor.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
"""Support for Aqualink temperature sensors."""
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
from iaqualink import AqualinkSensor
|
||||
|
||||
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.typing import HomeAssistantType
|
||||
|
||||
from . import AqualinkEntity
|
||||
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 sensors."""
|
||||
devs = []
|
||||
for dev in hass.data[AQUALINK_DOMAIN][DOMAIN]:
|
||||
devs.append(HassAqualinkSensor(dev))
|
||||
async_add_entities(devs, True)
|
||||
|
||||
|
||||
class HassAqualinkSensor(AqualinkEntity):
|
||||
"""Representation of a sensor."""
|
||||
|
||||
def __init__(self, dev: AqualinkSensor):
|
||||
"""Initialize the sensor."""
|
||||
self.dev = dev
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return self.dev.label
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the measurement unit for the sensor."""
|
||||
if self.dev.system.temp_unit == "F":
|
||||
return TEMP_FAHRENHEIT
|
||||
return TEMP_CELSIUS
|
||||
|
||||
@property
|
||||
def state(self) -> str:
|
||||
"""Return the state of the sensor."""
|
||||
return int(self.dev.state) if self.dev.state != "" else None
|
||||
|
||||
@property
|
||||
def device_class(self) -> Optional[str]:
|
||||
"""Return the class of the sensor."""
|
||||
if self.dev.name.endswith("_temp"):
|
||||
return DEVICE_CLASS_TEMPERATURE
|
||||
return None
|
Loading…
Add table
Reference in a new issue