2018-05-29 19:02:16 +00:00
|
|
|
"""
|
|
|
|
This platform provides binary sensors for key RainMachine data.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.rainmachine/
|
|
|
|
"""
|
|
|
|
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 (
|
2018-11-14 20:23:49 +00:00
|
|
|
BINARY_SENSORS, DATA_CLIENT, DOMAIN as RAINMACHINE_DOMAIN,
|
|
|
|
SENSOR_UPDATE_TOPIC, TYPE_FREEZE, TYPE_FREEZE_PROTECTION, TYPE_HOT_DAYS,
|
|
|
|
TYPE_HOURLY, TYPE_MONTH, TYPE_RAINDELAY, TYPE_RAINSENSOR, TYPE_WEEKDAY,
|
|
|
|
RainMachineEntity)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
DEPENDENCIES = ['rainmachine']
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2018-06-10 08:23:07 +00:00
|
|
|
async def async_setup_platform(
|
2018-08-24 14:37:30 +00:00
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2018-11-14 20:23:49 +00:00
|
|
|
"""Set up RainMachine binary sensors based on the old way."""
|
|
|
|
pass
|
|
|
|
|
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 = []
|
2018-11-14 20:23:49 +00:00
|
|
|
for sensor_type in rainmachine.binary_sensor_conditions:
|
2018-05-29 19:02:16 +00:00
|
|
|
name, icon = BINARY_SENSORS[sensor_type]
|
|
|
|
binary_sensors.append(
|
|
|
|
RainMachineBinarySensor(rainmachine, sensor_type, name, icon))
|
|
|
|
|
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."""
|
|
|
|
|
|
|
|
def __init__(self, rainmachine, sensor_type, name, icon):
|
|
|
|
"""Initialize the sensor."""
|
|
|
|
super().__init__(rainmachine)
|
|
|
|
|
|
|
|
self._icon = icon
|
|
|
|
self._name = name
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@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:
|
|
|
|
"""Return a unique, HASS-friendly identifier for this entity."""
|
|
|
|
return '{0}_{1}'.format(
|
|
|
|
self.rainmachine.device_mac.replace(':', ''), self._sensor_type)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Register callbacks."""
|
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)
|
|
|
|
|
2018-11-17 09:42:50 +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."""
|
|
|
|
if self._sensor_type == TYPE_FREEZE:
|
|
|
|
self._state = self.rainmachine.restrictions['current']['freeze']
|
|
|
|
elif self._sensor_type == TYPE_FREEZE_PROTECTION:
|
|
|
|
self._state = self.rainmachine.restrictions['global'][
|
|
|
|
'freezeProtectEnabled']
|
|
|
|
elif self._sensor_type == TYPE_HOT_DAYS:
|
|
|
|
self._state = self.rainmachine.restrictions['global'][
|
|
|
|
'hotDaysExtraWatering']
|
|
|
|
elif self._sensor_type == TYPE_HOURLY:
|
|
|
|
self._state = self.rainmachine.restrictions['current']['hourly']
|
|
|
|
elif self._sensor_type == TYPE_MONTH:
|
|
|
|
self._state = self.rainmachine.restrictions['current']['month']
|
|
|
|
elif self._sensor_type == TYPE_RAINDELAY:
|
|
|
|
self._state = self.rainmachine.restrictions['current']['rainDelay']
|
|
|
|
elif self._sensor_type == TYPE_RAINSENSOR:
|
|
|
|
self._state = self.rainmachine.restrictions['current'][
|
|
|
|
'rainSensor']
|
|
|
|
elif self._sensor_type == TYPE_WEEKDAY:
|
|
|
|
self._state = self.rainmachine.restrictions['current']['weekDay']
|