Skip to content

Commit

Permalink
feat: add entities for all controllers and valves
Browse files Browse the repository at this point in the history
  • Loading branch information
vanstinator committed May 22, 2020
1 parent 401ef52 commit 872c262
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
18 changes: 17 additions & 1 deletion custom_components/raincloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

from raincloudy.core import RainCloudy

from requests.exceptions import ConnectTimeout, HTTPError
import voluptuous as vol

Expand Down Expand Up @@ -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(
Expand Down
19 changes: 12 additions & 7 deletions custom_components/raincloud/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion custom_components/raincloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
14 changes: 10 additions & 4 deletions custom_components/raincloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions custom_components/raincloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -61,15 +63,15 @@ 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

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
Expand All @@ -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

Expand Down

0 comments on commit 872c262

Please sign in to comment.