2019-02-13 20:21:14 +00:00
|
|
|
"""Support for AquaLogic sensors."""
|
2018-10-02 05:32:03 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
2020-04-11 13:40:59 +00:00
|
|
|
POWER_WATT,
|
2020-02-28 19:46:48 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
UNIT_PERCENTAGE,
|
|
|
|
)
|
2018-10-02 05:32:03 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
|
|
|
from . import DOMAIN, UPDATE_TOPIC
|
2018-10-02 05:32:03 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
TEMP_UNITS = [TEMP_CELSIUS, TEMP_FAHRENHEIT]
|
2020-02-28 19:46:48 +00:00
|
|
|
PERCENT_UNITS = [UNIT_PERCENTAGE, UNIT_PERCENTAGE]
|
2019-07-31 19:25:30 +00:00
|
|
|
SALT_UNITS = ["g/L", "PPM"]
|
2020-04-11 13:40:59 +00:00
|
|
|
WATT_UNITS = [POWER_WATT, POWER_WATT]
|
2018-10-02 05:32:03 +00:00
|
|
|
NO_UNITS = [None, None]
|
|
|
|
|
|
|
|
# sensor_type [ description, unit, icon ]
|
|
|
|
# sensor_type corresponds to property names in aqualogic.core.AquaLogic
|
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"air_temp": ["Air Temperature", TEMP_UNITS, "mdi:thermometer"],
|
|
|
|
"pool_temp": ["Pool Temperature", TEMP_UNITS, "mdi:oil-temperature"],
|
|
|
|
"spa_temp": ["Spa Temperature", TEMP_UNITS, "mdi:oil-temperature"],
|
|
|
|
"pool_chlorinator": ["Pool Chlorinator", PERCENT_UNITS, "mdi:gauge"],
|
|
|
|
"spa_chlorinator": ["Spa Chlorinator", PERCENT_UNITS, "mdi:gauge"],
|
|
|
|
"salt_level": ["Salt Level", SALT_UNITS, "mdi:gauge"],
|
|
|
|
"pump_speed": ["Pump Speed", PERCENT_UNITS, "mdi:speedometer"],
|
|
|
|
"pump_power": ["Pump Power", WATT_UNITS, "mdi:gauge"],
|
|
|
|
"status": ["Status", NO_UNITS, "mdi:alert"],
|
2018-10-02 05:32:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-10-02 05:32:03 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-10-02 05:32:03 +00:00
|
|
|
"""Set up the sensor platform."""
|
|
|
|
sensors = []
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
processor = hass.data[DOMAIN]
|
2020-04-07 20:45:56 +00:00
|
|
|
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
2018-10-02 05:32:03 +00:00
|
|
|
sensors.append(AquaLogicSensor(processor, sensor_type))
|
|
|
|
|
|
|
|
async_add_entities(sensors)
|
|
|
|
|
|
|
|
|
|
|
|
class AquaLogicSensor(Entity):
|
|
|
|
"""Sensor implementation for the AquaLogic component."""
|
|
|
|
|
|
|
|
def __init__(self, processor, sensor_type):
|
|
|
|
"""Initialize sensor."""
|
|
|
|
self._processor = processor
|
|
|
|
self._type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2020-02-23 21:38:05 +00:00
|
|
|
return f"AquaLogic {SENSOR_TYPES[self._type][0]}"
|
2018-10-02 05:32:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement the value is expressed in."""
|
|
|
|
panel = self._processor.panel
|
|
|
|
if panel is None:
|
|
|
|
return None
|
|
|
|
if panel.is_metric:
|
|
|
|
return SENSOR_TYPES[self._type][1][0]
|
|
|
|
return SENSOR_TYPES[self._type][1][1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Return the polling state."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return SENSOR_TYPES[self._type][2]
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2020-04-02 16:25:33 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
|
|
|
UPDATE_TOPIC, self.async_update_callback
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-02 05:32:03 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_callback(self):
|
|
|
|
"""Update callback."""
|
|
|
|
panel = self._processor.panel
|
|
|
|
if panel is not None:
|
|
|
|
self._state = getattr(panel, self._type)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|