Skip to content
57 changes: 50 additions & 7 deletions homeassistant/components/rainmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
https://home-assistant.io/components/rainmachine/
"""
import logging
from datetime import timedelta

import voluptuous as vol

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

REQUIREMENTS = ['regenmaschine==0.4.1']

Expand All @@ -26,11 +27,11 @@
CONF_ZONE_RUN_TIME = 'zone_run_time'

DEFAULT_ATTRIBUTION = 'Data provided by Green Electronics LLC'
DEFAULT_ICON = 'mdi:water'
DEFAULT_PORT = 8080
DEFAULT_SSL = True

MIN_SCAN_TIME = timedelta(seconds=1)
MIN_SCAN_TIME_FORCED = timedelta(milliseconds=100)
PROGRAM_UPDATE_TOPIC = '{0}_program_update'.format(DOMAIN)

SWITCH_SCHEMA = vol.Schema({
vol.Optional(CONF_ZONE_RUN_TIME):
Expand Down Expand Up @@ -68,8 +69,7 @@ def setup(hass, config):
auth = Authenticator.create_local(
ip_address, password, port=port, https=ssl)
client = Client(auth)
mac = client.provision.wifi()['macAddress']
hass.data[DATA_RAINMACHINE] = (client, mac)
hass.data[DATA_RAINMACHINE] = RainMachine(client)
except (HTTPError, ConnectTimeout, UnboundLocalError) as exc_info:
_LOGGER.error('An error occurred: %s', str(exc_info))
hass.components.persistent_notification.create(
Expand All @@ -87,3 +87,46 @@ def setup(hass, config):
_LOGGER.debug('Setup complete')

return True


class RainMachine(object):
"""Define a generic RainMachine object."""

def __init__(self, client):
"""Initialize."""
self.client = client
self.device_mac = self.client.provision.wifi()['macAddress']


class RainMachineEntity(Entity):
"""Define a generic RainMachine entity."""

def __init__(self,
rainmachine,
rainmachine_type,
rainmachine_entity_id,
icon=DEFAULT_ICON):
"""Initialize."""
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
self._icon = icon
self._rainmachine_type = rainmachine_type
self._rainmachine_entity_id = rainmachine_entity_id
self.rainmachine = rainmachine

@property
def device_state_attributes(self) -> dict:
"""Return the state attributes."""
return self._attrs

@property
def icon(self) -> str:
"""Return the icon."""
return self._icon

@property
def unique_id(self) -> str:
"""Return a unique, HASS-friendly identifier for this entity."""
return '{0}_{1}_{2}'.format(
self.rainmachine.device_mac.replace(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just use mac and type. I don't know what entity id is ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MAC and type aren't sufficient – in that case, every program would have the same unique ID (e.g., "abcdef1234_program"). Same with zones. entity_id is the ID of that specific program or zone, which gives results like "abcdef1234_program_2". Let me know if I'm thinking about this wrong or doing the job in a confusing manner.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Got confused because Home Assistant also has the concept of entity ids.

':', ''), self._rainmachine_type,
self._rainmachine_entity_id)
Loading