Skip to content

Commit

Permalink
Add support for multiple RainMachine controllers (#18989)
Browse files Browse the repository at this point in the history
* Add support for multiple RainMachine controllers

* Member comments

* Member comments

* Member comments

* Cleanup

* More config flow cleanup

* Member comments
  • Loading branch information
bachya authored Dec 5, 2018
1 parent bc69309 commit 0870254
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
68 changes: 36 additions & 32 deletions homeassistant/components/rainmachine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from homeassistant.helpers.event import async_track_time_interval

from .config_flow import configured_instances
from .const import DATA_CLIENT, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DOMAIN
from .const import (
DATA_CLIENT, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DEFAULT_SSL, DOMAIN)

REQUIREMENTS = ['regenmaschine==1.0.7']

Expand All @@ -33,13 +34,13 @@
SENSOR_UPDATE_TOPIC = '{0}_data_update'.format(DOMAIN)
ZONE_UPDATE_TOPIC = '{0}_zone_update'.format(DOMAIN)

CONF_CONTROLLERS = 'controllers'
CONF_PROGRAM_ID = 'program_id'
CONF_ZONE_ID = 'zone_id'
CONF_ZONE_RUN_TIME = 'zone_run_time'

DEFAULT_ATTRIBUTION = 'Data provided by Green Electronics LLC'
DEFAULT_ICON = 'mdi:water'
DEFAULT_SSL = True
DEFAULT_ZONE_RUN = 60 * 10

TYPE_FREEZE = 'freeze'
Expand Down Expand Up @@ -97,23 +98,26 @@

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_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL):
cv.time_period,
vol.Optional(CONF_BINARY_SENSORS, default={}):
BINARY_SENSOR_SCHEMA,
vol.Optional(CONF_SENSORS, default={}): SENSOR_SCHEMA,
vol.Optional(CONF_SWITCHES, default={}): SWITCH_SCHEMA,
})
},
extra=vol.ALLOW_EXTRA)

CONTROLLER_SCHEMA = 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_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL):
cv.time_period,
vol.Optional(CONF_BINARY_SENSORS, default={}): BINARY_SENSOR_SCHEMA,
vol.Optional(CONF_SENSORS, default={}): SENSOR_SCHEMA,
vol.Optional(CONF_SWITCHES, default={}): SWITCH_SCHEMA,
})


CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_CONTROLLERS):
vol.All(cv.ensure_list, [CONTROLLER_SCHEMA]),
}),
}, extra=vol.ALLOW_EXTRA)


async def async_setup(hass, config):
Expand All @@ -127,14 +131,15 @@ async def async_setup(hass, config):

conf = config[DOMAIN]

if conf[CONF_IP_ADDRESS] in configured_instances(hass):
return True
for controller in conf[CONF_CONTROLLERS]:
if controller[CONF_IP_ADDRESS] in configured_instances(hass):
continue

hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={'source': SOURCE_IMPORT},
data=conf))
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={'source': SOURCE_IMPORT},
data=controller))

return True

Expand All @@ -144,16 +149,15 @@ async def async_setup_entry(hass, config_entry):
from regenmaschine import login
from regenmaschine.errors import RainMachineError

ip_address = config_entry.data[CONF_IP_ADDRESS]
password = config_entry.data[CONF_PASSWORD]
port = config_entry.data[CONF_PORT]
ssl = config_entry.data.get(CONF_SSL, DEFAULT_SSL)

websession = aiohttp_client.async_get_clientsession(hass)

try:
client = await login(
ip_address, password, websession, port=port, ssl=ssl)
config_entry.data[CONF_IP_ADDRESS],
config_entry.data[CONF_PASSWORD],
websession,
port=config_entry.data[CONF_PORT],
ssl=config_entry.data[CONF_SSL])
rainmachine = RainMachine(
client,
config_entry.data.get(CONF_BINARY_SENSORS, {}).get(
Expand Down
10 changes: 8 additions & 2 deletions homeassistant/components/rainmachine/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.const import (
CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SCAN_INTERVAL)
CONF_IP_ADDRESS, CONF_PASSWORD, CONF_PORT, CONF_SCAN_INTERVAL, CONF_SSL)
from homeassistant.helpers import aiohttp_client

from .const import DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DOMAIN
from .const import DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DEFAULT_SSL, DOMAIN


@callback
Expand Down Expand Up @@ -74,6 +74,12 @@ async def async_step_user(self, user_input=None):
CONF_PASSWORD: 'invalid_credentials'
})

# Since the config entry doesn't allow for configuration of SSL, make
# sure it's set:
if user_input.get(CONF_SSL) is None:
user_input[CONF_SSL] = DEFAULT_SSL

# Timedeltas are easily serializable, so store the seconds instead:
scan_interval = user_input.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
user_input[CONF_SCAN_INTERVAL] = scan_interval.seconds
Expand Down
1 change: 1 addition & 0 deletions homeassistant/components/rainmachine/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@

DEFAULT_PORT = 8080
DEFAULT_SCAN_INTERVAL = timedelta(seconds=60)
DEFAULT_SSL = True

TOPIC_UPDATE = 'update_{0}'

0 comments on commit 0870254

Please sign in to comment.