2019-04-03 15:40:03 +00:00
|
|
|
"""This platform provides binary sensors for key RainMachine data."""
|
2018-05-29 19:02:16 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_CLIENT,
|
|
|
|
DOMAIN as RAINMACHINE_DOMAIN,
|
|
|
|
PROVISION_SETTINGS,
|
|
|
|
RESTRICTIONS_CURRENT,
|
|
|
|
RESTRICTIONS_UNIVERSAL,
|
|
|
|
SENSOR_UPDATE_TOPIC,
|
|
|
|
RainMachineEntity,
|
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
TYPE_FLOW_SENSOR = "flow_sensor"
|
|
|
|
TYPE_FREEZE = "freeze"
|
|
|
|
TYPE_FREEZE_PROTECTION = "freeze_protection"
|
|
|
|
TYPE_HOT_DAYS = "extra_water_on_hot_days"
|
|
|
|
TYPE_HOURLY = "hourly"
|
|
|
|
TYPE_MONTH = "month"
|
|
|
|
TYPE_RAINDELAY = "raindelay"
|
|
|
|
TYPE_RAINSENSOR = "rainsensor"
|
|
|
|
TYPE_WEEKDAY = "weekday"
|
|
|
|
|
|
|
|
BINARY_SENSORS = {
|
|
|
|
TYPE_FLOW_SENSOR: ("Flow Sensor", "mdi:water-pump", True),
|
|
|
|
TYPE_FREEZE: ("Freeze Restrictions", "mdi:cancel", True),
|
|
|
|
TYPE_FREEZE_PROTECTION: ("Freeze Protection", "mdi:weather-snowy", True),
|
|
|
|
TYPE_HOT_DAYS: ("Extra Water on Hot Days", "mdi:thermometer-lines", True),
|
|
|
|
TYPE_HOURLY: ("Hourly Restrictions", "mdi:cancel", False),
|
|
|
|
TYPE_MONTH: ("Month Restrictions", "mdi:cancel", False),
|
|
|
|
TYPE_RAINDELAY: ("Rain Delay Restrictions", "mdi:cancel", False),
|
|
|
|
TYPE_RAINSENSOR: ("Rain Sensor Restrictions", "mdi:cancel", False),
|
|
|
|
TYPE_WEEKDAY: ("Weekday Restrictions", "mdi:cancel", False),
|
|
|
|
}
|
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2018-11-14 20:23:49 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up RainMachine binary sensors based on a config entry."""
|
|
|
|
rainmachine = hass.data[RAINMACHINE_DOMAIN][DATA_CLIENT][entry.entry_id]
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
binary_sensors = []
|
2020-01-23 04:49:47 +00:00
|
|
|
for sensor_type, (name, icon, enabled_by_default) in BINARY_SENSORS.items():
|
2018-05-29 19:02:16 +00:00
|
|
|
binary_sensors.append(
|
2020-01-23 04:49:47 +00:00
|
|
|
RainMachineBinarySensor(
|
|
|
|
rainmachine, sensor_type, name, icon, enabled_by_default
|
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(binary_sensors, True)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RainMachineBinarySensor(RainMachineEntity, BinarySensorDevice):
|
|
|
|
"""A sensor implementation for raincloud device."""
|
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
def __init__(self, rainmachine, sensor_type, name, icon, enabled_by_default):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(rainmachine)
|
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
self._enabled_by_default = enabled_by_default
|
2018-05-29 19:02:16 +00:00
|
|
|
self._icon = icon
|
|
|
|
self._name = name
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
|
2020-01-23 04:49:47 +00:00
|
|
|
@property
|
|
|
|
def entity_registry_enabled_default(self):
|
|
|
|
"""Determine whether an entity is enabled by default."""
|
|
|
|
return self._enabled_by_default
|
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return the status of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Disable polling."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
2020-01-05 12:09:17 +00:00
|
|
|
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "{0}_{1}".format(
|
|
|
|
self.rainmachine.device_mac.replace(":", ""), self._sensor_type
|
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-11-14 20:23:49 +00:00
|
|
|
@callback
|
2018-11-29 21:28:27 +00:00
|
|
|
def update():
|
2018-11-14 20:23:49 +00:00
|
|
|
"""Update the state."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._dispatcher_handlers.append(
|
|
|
|
async_dispatcher_connect(self.hass, SENSOR_UPDATE_TOPIC, update)
|
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_update(self):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Update the state."""
|
2019-05-02 07:45:51 +00:00
|
|
|
if self._sensor_type == TYPE_FLOW_SENSOR:
|
2020-01-07 17:43:12 +00:00
|
|
|
self._state = self.rainmachine.data[PROVISION_SETTINGS]["system"].get(
|
|
|
|
"useFlowSensor"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
elif self._sensor_type == TYPE_FREEZE:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["freeze"]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_FREEZE_PROTECTION:
|
2019-05-02 07:45:51 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_UNIVERSAL][
|
2019-07-31 19:25:30 +00:00
|
|
|
"freezeProtectEnabled"
|
|
|
|
]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_HOT_DAYS:
|
2019-05-02 07:45:51 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_UNIVERSAL][
|
2019-07-31 19:25:30 +00:00
|
|
|
"hotDaysExtraWatering"
|
|
|
|
]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_HOURLY:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["hourly"]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_MONTH:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["month"]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_RAINDELAY:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["rainDelay"]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_RAINSENSOR:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["rainSensor"]
|
2018-05-29 19:02:16 +00:00
|
|
|
elif self._sensor_type == TYPE_WEEKDAY:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_CURRENT]["weekDay"]
|