From 872c262fb90de02e0ba890387e84ac5407cc0883 Mon Sep 17 00:00:00 2001 From: Justin Vanderhooft Date: Thu, 21 May 2020 23:07:14 -0400 Subject: [PATCH] feat: add entities for all controllers and valves --- custom_components/raincloud/__init__.py | 18 +++++++++++++++++- custom_components/raincloud/binary_sensor.py | 19 ++++++++++++------- custom_components/raincloud/manifest.json | 2 +- custom_components/raincloud/sensor.py | 14 ++++++++++---- custom_components/raincloud/switch.py | 12 +++++++----- 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/custom_components/raincloud/__init__.py b/custom_components/raincloud/__init__.py index 3daea7a..6669cee 100644 --- a/custom_components/raincloud/__init__.py +++ b/custom_components/raincloud/__init__.py @@ -3,6 +3,7 @@ import logging from raincloudy.core import RainCloudy + from requests.exceptions import ConnectTimeout, HTTPError import voluptuous as vol @@ -140,14 +141,29 @@ def __init__(self, data, sensor_type): """Initialize the RainCloud entity.""" self.data = data self._sensor_type = sensor_type - self._name = f"{self.data.name} {KEY_MAP.get(self._sensor_type)}" self._state = None + if hasattr(self.data, '_faucet'): + self._name = f"{self.data._faucet.id}: Zone {self.data.id} {KEY_MAP.get(self._sensor_type)}" + else: + self._name = f"{self.data.id} {KEY_MAP.get(self._sensor_type)}" + @property def name(self): """Return the name of the sensor.""" return self._name + @property + def unique_id(self): + """Return the serial combination to create a unique identifier""" + + if hasattr(self.data, '_faucet'): + _LOGGER.warning(f"{self.data._faucet.serial}_{self._sensor_type}_{self.data.id}") + return f"{self.data._faucet.serial}_{self._sensor_type}_{self.data.id}" + + _LOGGER.error(f"{self.data.id}_{self._sensor_type}") + return f"{self.data.serial}_{self._sensor_type}" + async def async_added_to_hass(self): """Register callbacks.""" self.async_on_remove( diff --git a/custom_components/raincloud/binary_sensor.py b/custom_components/raincloud/binary_sensor.py index 3887a7c..08dc17b 100644 --- a/custom_components/raincloud/binary_sensor.py +++ b/custom_components/raincloud/binary_sensor.py @@ -27,15 +27,20 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensors = [] for sensor_type in config.get(CONF_MONITORED_CONDITIONS): if sensor_type == "status": - sensors.append(RainCloudBinarySensor(raincloud.controller, sensor_type)) - sensors.append( - RainCloudBinarySensor(raincloud.controller.faucet, sensor_type) - ) + for controller in raincloud.controllers: + sensors.append(RainCloudBinarySensor(controller, sensor_type)) + + for faucet in controller.faucets: + sensors.append( + RainCloudBinarySensor(faucet, sensor_type) + ) else: - # create a sensor for each zone managed by faucet - for zone in raincloud.controller.faucet.zones: - sensors.append(RainCloudBinarySensor(zone, sensor_type)) + # create a sensor for each zone managed by controller and faucet + for controller in raincloud.controllers: + for faucet in controller.faucets: + for zone in faucet.zones: + sensors.append(RainCloudBinarySensor(zone, sensor_type)) add_entities(sensors, True) return True diff --git a/custom_components/raincloud/manifest.json b/custom_components/raincloud/manifest.json index 27427c6..b56ee8b 100644 --- a/custom_components/raincloud/manifest.json +++ b/custom_components/raincloud/manifest.json @@ -2,6 +2,6 @@ "domain": "raincloud", "name": "Melnor RainCloud", "documentation": "https://www.home-assistant.io/integrations/raincloud", - "requirements": ["raincloudy==0.0.7"], + "requirements": ["raincloudy==1.0a1"], "codeowners": ["@vanstinator"] } \ No newline at end of file diff --git a/custom_components/raincloud/sensor.py b/custom_components/raincloud/sensor.py index b96eda0..374b622 100644 --- a/custom_components/raincloud/sensor.py +++ b/custom_components/raincloud/sensor.py @@ -28,11 +28,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensors = [] for sensor_type in config.get(CONF_MONITORED_CONDITIONS): if sensor_type == "battery": - sensors.append(RainCloudSensor(raincloud.controller.faucet, sensor_type)) + for controller in raincloud.controllers: + for faucet in controller.faucets: + sensors.append(RainCloudSensor(faucet, sensor_type)) + else: - # create a sensor for each zone managed by a faucet - for zone in raincloud.controller.faucet.zones: - sensors.append(RainCloudSensor(zone, sensor_type)) + # create a sensor for each zone managed by controller and faucet + for controller in raincloud.controllers: + for faucet in controller.faucets: + for zone in faucet.zones: + sensors.append(RainCloudSensor(zone, sensor_type)) + add_entities(sensors, True) return True diff --git a/custom_components/raincloud/switch.py b/custom_components/raincloud/switch.py index 0e07427..3739ed5 100644 --- a/custom_components/raincloud/switch.py +++ b/custom_components/raincloud/switch.py @@ -39,8 +39,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None): sensors = [] for sensor_type in config.get(CONF_MONITORED_CONDITIONS): # create a sensor for each zone managed by faucet - for zone in raincloud.controller.faucet.zones: - sensors.append(RainCloudSwitch(default_watering_timer, zone, sensor_type)) + for controller in raincloud.controllers: + for faucet in controller.faucets: + for zone in faucet.zones: + sensors.append(RainCloudSwitch(default_watering_timer, zone, sensor_type)) add_entities(sensors, True) @@ -61,7 +63,7 @@ def is_on(self): def turn_on(self, **kwargs): """Turn the device on.""" if self._sensor_type == "manual_watering": - self.data.watering_time = self._default_watering_timer + self.data.manual_watering = self._default_watering_timer elif self._sensor_type == "auto_watering": self.data.auto_watering = True self._state = True @@ -69,7 +71,7 @@ def turn_on(self, **kwargs): def turn_off(self, **kwargs): """Turn the device off.""" if self._sensor_type == "manual_watering": - self.data.watering_time = "off" + self.data.manual_watering = "off" elif self._sensor_type == "auto_watering": self.data.auto_watering = False self._state = False @@ -78,7 +80,7 @@ def update(self): """Update device state.""" _LOGGER.debug("Updating RainCloud switch: %s", self._name) if self._sensor_type == "manual_watering": - self._state = bool(self.data.watering_time) + self._state = bool(self.data.manual_watering) elif self._sensor_type == "auto_watering": self._state = self.data.auto_watering