Skip to content

Commit

Permalink
Converts RainMachine to hub model (part 2) (#14225)
Browse files Browse the repository at this point in the history
* Converts RainMachine to hub model (part 2)

* Small style adjustments for consistency

* Moving MAC calculation to one-time call in component

* Removing unneeded attribute

* Bumping Travis

* Lint
  • Loading branch information
bachya authored and balloob committed May 1, 2018
1 parent 8d5c3a2 commit 7a05471
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
26 changes: 22 additions & 4 deletions homeassistant/components/rainmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from datetime import timedelta

import voluptuous as vol
from requests.exceptions import ConnectTimeout

from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.const import (
CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SSL)
CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SSL, CONF_SWITCHES)

REQUIREMENTS = ['regenmaschine==0.4.1']

Expand All @@ -24,20 +23,28 @@
NOTIFICATION_ID = 'rainmachine_notification'
NOTIFICATION_TITLE = 'RainMachine Component Setup'

CONF_ZONE_RUN_TIME = 'zone_run_time'

DEFAULT_ATTRIBUTION = 'Data provided by Green Electronics LLC'
DEFAULT_PORT = 8080
DEFAULT_SSL = True

MIN_SCAN_TIME = timedelta(seconds=1)
MIN_SCAN_TIME_FORCED = timedelta(milliseconds=100)

SWITCH_SCHEMA = vol.Schema({
vol.Optional(CONF_ZONE_RUN_TIME):
cv.positive_int
})

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema({
vol.Required(CONF_IP_ADDRESS): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
vol.Optional(CONF_SWITCHES): SWITCH_SCHEMA,
})
},
extra=vol.ALLOW_EXTRA)
Expand All @@ -47,18 +54,22 @@ def setup(hass, config):
"""Set up the RainMachine component."""
from regenmaschine import Authenticator, Client
from regenmaschine.exceptions import HTTPError
from requests.exceptions import ConnectTimeout

conf = config[DOMAIN]
ip_address = conf[CONF_IP_ADDRESS]
password = conf[CONF_PASSWORD]
port = conf[CONF_PORT]
ssl = conf[CONF_SSL]

_LOGGER.debug('Setting up RainMachine client')

try:
auth = Authenticator.create_local(
ip_address, password, port=port, https=ssl)
client = Client(auth)
hass.data[DATA_RAINMACHINE] = client
mac = client.provision.wifi()['macAddress']
hass.data[DATA_RAINMACHINE] = (client, mac)
except (HTTPError, ConnectTimeout, UnboundLocalError) as exc_info:
_LOGGER.error('An error occurred: %s', str(exc_info))
hass.components.persistent_notification.create(
Expand All @@ -68,4 +79,11 @@ def setup(hass, config):
title=NOTIFICATION_TITLE,
notification_id=NOTIFICATION_ID)
return False

_LOGGER.debug('Setting up switch platform')
switch_config = conf.get(CONF_SWITCHES, {})
discovery.load_platform(hass, 'switch', DOMAIN, switch_config, config)

_LOGGER.debug('Setup complete')

return True
47 changes: 19 additions & 28 deletions homeassistant/components/switch/rainmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,33 @@

from logging import getLogger

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.rainmachine import (
DATA_RAINMACHINE, DEFAULT_ATTRIBUTION, MIN_SCAN_TIME, MIN_SCAN_TIME_FORCED)
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import ATTR_ATTRIBUTION, ATTR_DEVICE_CLASS
CONF_ZONE_RUN_TIME, DATA_RAINMACHINE, DEFAULT_ATTRIBUTION, MIN_SCAN_TIME,
MIN_SCAN_TIME_FORCED)
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import ATTR_ATTRIBUTION
from homeassistant.util import Throttle

_LOGGER = getLogger(__name__)
DEPENDENCIES = ['rainmachine']

_LOGGER = getLogger(__name__)

ATTR_CYCLES = 'cycles'
ATTR_TOTAL_DURATION = 'total_duration'

CONF_ZONE_RUN_TIME = 'zone_run_time'

DEFAULT_ZONE_RUN_SECONDS = 60 * 10

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_ZONE_RUN_TIME, default=DEFAULT_ZONE_RUN_SECONDS):
cv.positive_int
})
DEFAULT_ZONE_RUN = 60 * 10


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set this component up under its platform."""
client = hass.data.get(DATA_RAINMACHINE)
device_name = client.provision.device_name()['name']
device_mac = client.provision.wifi()['macAddress']
if discovery_info is None:
return

_LOGGER.debug('Config received: %s', discovery_info)

_LOGGER.debug('Config received: %s', config)
zone_run_time = discovery_info.get(CONF_ZONE_RUN_TIME, DEFAULT_ZONE_RUN)

zone_run_time = config[CONF_ZONE_RUN_TIME]
client, device_mac = hass.data.get(DATA_RAINMACHINE)

entities = []
for program in client.programs.all().get('programs', {}):
Expand All @@ -44,15 +37,15 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

_LOGGER.debug('Adding program: %s', program)
entities.append(
RainMachineProgram(client, device_name, device_mac, program))
RainMachineProgram(client, device_mac, program))

for zone in client.zones.all().get('zones', {}):
if not zone.get('active'):
continue

_LOGGER.debug('Adding zone: %s', zone)
entities.append(
RainMachineZone(client, device_name, device_mac, zone,
RainMachineZone(client, device_mac, zone,
zone_run_time))

add_devices(entities, True)
Expand All @@ -61,18 +54,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class RainMachineEntity(SwitchDevice):
"""A class to represent a generic RainMachine entity."""

def __init__(self, client, device_name, device_mac, entity_json):
def __init__(self, client, device_mac, entity_json):
"""Initialize a generic RainMachine entity."""
self._api_type = 'remote' if client.auth.using_remote_api else 'local'
self._client = client
self._entity_json = entity_json

self.device_mac = device_mac
self.device_name = device_name

self._attrs = {
ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION,
ATTR_DEVICE_CLASS: self.device_name
ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION
}

@property
Expand Down Expand Up @@ -156,10 +147,10 @@ def update(self) -> None:
class RainMachineZone(RainMachineEntity):
"""A RainMachine zone."""

def __init__(self, client, device_name, device_mac, zone_json,
def __init__(self, client, device_mac, zone_json,
zone_run_time):
"""Initialize a RainMachine zone."""
super().__init__(client, device_name, device_mac, zone_json)
super().__init__(client, device_mac, zone_json)
self._run_time = zone_run_time
self._attrs.update({
ATTR_CYCLES: self._entity_json.get('noOfCycles'),
Expand Down

0 comments on commit 7a05471

Please sign in to comment.