2019-04-03 15:40:03 +00:00
|
|
|
"""This platform provides support for sensor data from RainMachine."""
|
2018-05-29 19:02:16 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_CLIENT,
|
|
|
|
DOMAIN as RAINMACHINE_DOMAIN,
|
|
|
|
PROVISION_SETTINGS,
|
|
|
|
RESTRICTIONS_UNIVERSAL,
|
|
|
|
SENSOR_UPDATE_TOPIC,
|
|
|
|
SENSORS,
|
|
|
|
TYPE_FLOW_SENSOR_CLICK_M3,
|
|
|
|
TYPE_FLOW_SENSOR_CONSUMED_LITERS,
|
|
|
|
TYPE_FLOW_SENSOR_START_INDEX,
|
|
|
|
TYPE_FLOW_SENSOR_WATERING_CLICKS,
|
|
|
|
TYPE_FREEZE_TEMP,
|
|
|
|
RainMachineEntity,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2018-05-29 19:02:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2018-11-14 20:23:49 +00:00
|
|
|
"""Set up RainMachine 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 sensors based on a config entry."""
|
|
|
|
rainmachine = hass.data[RAINMACHINE_DOMAIN][DATA_CLIENT][entry.entry_id]
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
sensors = []
|
2018-11-14 20:23:49 +00:00
|
|
|
for sensor_type in rainmachine.sensor_conditions:
|
2019-06-21 23:12:28 +00:00
|
|
|
name, icon, unit, device_class = SENSORS[sensor_type]
|
2018-05-29 19:02:16 +00:00
|
|
|
sensors.append(
|
2019-07-31 19:25:30 +00:00
|
|
|
RainMachineSensor(rainmachine, sensor_type, name, icon, unit, device_class)
|
|
|
|
)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(sensors, True)
|
2018-05-29 19:02:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RainMachineSensor(RainMachineEntity):
|
|
|
|
"""A sensor implementation for raincloud device."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(self, rainmachine, sensor_type, name, icon, unit, device_class):
|
2018-05-29 19:02:16 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(rainmachine)
|
|
|
|
|
2019-06-21 23:12:28 +00:00
|
|
|
self._device_class = device_class
|
2018-05-29 19:02:16 +00:00
|
|
|
self._icon = icon
|
|
|
|
self._name = name
|
|
|
|
self._sensor_type = sensor_type
|
|
|
|
self._state = None
|
|
|
|
self._unit = unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Disable polling."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self) -> str:
|
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return a unique, HASS-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
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit the value is expressed in."""
|
|
|
|
return self._unit
|
|
|
|
|
|
|
|
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:47:41 +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 sensor's state."""
|
2019-05-02 07:45:51 +00:00
|
|
|
if self._sensor_type == TYPE_FLOW_SENSOR_CLICK_M3:
|
|
|
|
self._state = self.rainmachine.data[PROVISION_SETTINGS].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorClicksPerCubicMeter"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
elif self._sensor_type == TYPE_FLOW_SENSOR_CONSUMED_LITERS:
|
|
|
|
clicks = self.rainmachine.data[PROVISION_SETTINGS].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorWateringClicks"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
clicks_per_m3 = self.rainmachine.data[PROVISION_SETTINGS].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorClicksPerCubicMeter"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
|
|
|
|
if clicks and clicks_per_m3:
|
|
|
|
self._state = (clicks * 1000) / clicks_per_m3
|
|
|
|
else:
|
|
|
|
self._state = None
|
|
|
|
elif self._sensor_type == TYPE_FLOW_SENSOR_START_INDEX:
|
|
|
|
self._state = self.rainmachine.data[PROVISION_SETTINGS].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorStartIndex"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
elif self._sensor_type == TYPE_FLOW_SENSOR_WATERING_CLICKS:
|
|
|
|
self._state = self.rainmachine.data[PROVISION_SETTINGS].get(
|
2019-07-31 19:25:30 +00:00
|
|
|
"flowSensorWateringClicks"
|
|
|
|
)
|
2019-05-02 07:45:51 +00:00
|
|
|
elif self._sensor_type == TYPE_FREEZE_TEMP:
|
|
|
|
self._state = self.rainmachine.data[RESTRICTIONS_UNIVERSAL][
|
2019-07-31 19:25:30 +00:00
|
|
|
"freezeProtectTemp"
|
|
|
|
]
|