diff --git a/homeassistant/components/rainmachine/__init__.py b/homeassistant/components/rainmachine/__init__.py index 928c2ab2027036..6e1b8b68437a83 100644 --- a/homeassistant/components/rainmachine/__init__.py +++ b/homeassistant/components/rainmachine/__init__.py @@ -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'] @@ -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' @@ -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): @@ -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 @@ -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( diff --git a/homeassistant/components/rainmachine/config_flow.py b/homeassistant/components/rainmachine/config_flow.py index ecf497333cbd34..59b27fe0099f3f 100644 --- a/homeassistant/components/rainmachine/config_flow.py +++ b/homeassistant/components/rainmachine/config_flow.py @@ -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 @@ -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 diff --git a/homeassistant/components/rainmachine/const.py b/homeassistant/components/rainmachine/const.py index ec1f0436ccb18a..e0e79e8c160ed4 100644 --- a/homeassistant/components/rainmachine/const.py +++ b/homeassistant/components/rainmachine/const.py @@ -10,5 +10,6 @@ DEFAULT_PORT = 8080 DEFAULT_SCAN_INTERVAL = timedelta(seconds=60) +DEFAULT_SSL = True TOPIC_UPDATE = 'update_{0}'