From 897862fca4f4152f7baf75aee5172808c6137143 Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 13 Mar 2019 08:19:26 -0600 Subject: [PATCH] Add availability and next run datetime to RainMachine switches (#21786) --- .../components/rainmachine/__init__.py | 2 +- .../components/rainmachine/switch.py | 41 ++++++++++--------- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/rainmachine/__init__.py b/homeassistant/components/rainmachine/__init__.py index 6da5b2d6c10..78083e0d9da 100644 --- a/homeassistant/components/rainmachine/__init__.py +++ b/homeassistant/components/rainmachine/__init__.py @@ -19,7 +19,7 @@ from .config_flow import configured_instances from .const import ( DATA_CLIENT, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DEFAULT_SSL, DOMAIN) -REQUIREMENTS = ['regenmaschine==1.2.0'] +REQUIREMENTS = ['regenmaschine==1.4.0'] _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/rainmachine/switch.py b/homeassistant/components/rainmachine/switch.py index b48cc0a1e14..e3a1ddab912 100644 --- a/homeassistant/components/rainmachine/switch.py +++ b/homeassistant/components/rainmachine/switch.py @@ -5,6 +5,7 @@ For more details about this component, please refer to the documentation at https://home-assistant.io/components/switch.rainmachine/ """ import logging +from datetime import datetime from homeassistant.components.rainmachine import ( DATA_CLIENT, DOMAIN as RAINMACHINE_DOMAIN, PROGRAM_UPDATE_TOPIC, @@ -19,6 +20,7 @@ DEPENDENCIES = ['rainmachine'] _LOGGER = logging.getLogger(__name__) +ATTR_NEXT_RUN = 'next_run' ATTR_AREA = 'area' ATTR_CS_ON = 'cs_on' ATTR_CURRENT_CYCLE = 'current_cycle' @@ -111,20 +113,12 @@ async def async_setup_entry(hass, entry, async_add_entities): entities = [] - programs = await rainmachine.client.programs.all() + programs = await rainmachine.client.programs.all(include_inactive=True) for program in programs: - if not program.get('active'): - continue - - _LOGGER.debug('Adding program: %s', program) entities.append(RainMachineProgram(rainmachine, program)) - zones = await rainmachine.client.zones.all() + zones = await rainmachine.client.zones.all(include_inactive=True) for zone in zones: - if not zone.get('active'): - continue - - _LOGGER.debug('Adding zone: %s', zone) entities.append( RainMachineZone( rainmachine, zone, rainmachine.default_zone_runtime)) @@ -144,16 +138,16 @@ class RainMachineSwitch(RainMachineEntity, SwitchDevice): self._rainmachine_entity_id = obj['uid'] self._switch_type = switch_type + @property + def available(self) -> bool: + """Return True if entity is available.""" + return bool(self._obj.get('active')) + @property def icon(self) -> str: """Return the icon.""" return 'mdi:water' - @property - def is_enabled(self) -> bool: - """Return whether the entity is enabled.""" - return self._obj.get('active') - @property def unique_id(self) -> str: """Return a unique, HASS-friendly identifier for this entity.""" @@ -222,8 +216,17 @@ class RainMachineProgram(RainMachineSwitch): self._obj = await self.rainmachine.client.programs.get( self._rainmachine_entity_id) + try: + next_run = datetime.strptime( + '{0} {1}'.format( + self._obj['nextRun'], self._obj['startTime']), + '%Y-%m-%d %H:%M').isoformat() + except ValueError: + next_run = None + self._attrs.update({ ATTR_ID: self._obj['uid'], + ATTR_NEXT_RUN: next_run, ATTR_SOAK: self._obj.get('soak'), ATTR_STATUS: PROGRAM_STATUS_MAP[self._obj.get('status')], ATTR_ZONES: ', '.join(z['name'] for z in self.zones) @@ -297,13 +300,13 @@ class RainMachineZone(RainMachineSwitch): ATTR_CURRENT_CYCLE: self._obj.get('cycle'), ATTR_FIELD_CAPACITY: - self._properties_json.get('waterSense') - .get('fieldCapacity'), + self._properties_json.get('waterSense').get( + 'fieldCapacity'), ATTR_NO_CYCLES: self._obj.get('noOfCycles'), ATTR_PRECIP_RATE: - self._properties_json.get('waterSense') - .get('precipitationRate'), + self._properties_json.get('waterSense').get( + 'precipitationRate'), ATTR_RESTRICTIONS: self._obj.get('restriction'), ATTR_SLOPE: diff --git a/requirements_all.txt b/requirements_all.txt index dc6b2e17d94..8c889923859 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1505,7 +1505,7 @@ raspyrfm-client==1.2.8 recollect-waste==1.0.1 # homeassistant.components.rainmachine -regenmaschine==1.2.0 +regenmaschine==1.4.0 # homeassistant.components.python_script restrictedpython==4.0b8 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index cf104aff669..b4fb923d995 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -261,7 +261,7 @@ pyunifi==2.16 pywebpush==1.6.0 # homeassistant.components.rainmachine -regenmaschine==1.2.0 +regenmaschine==1.4.0 # homeassistant.components.python_script restrictedpython==4.0b8