Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions homeassistant/components/binary_sensor/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMBinarySensor(hass, conf)
new_device.link_homematic()
new_device = HMBinarySensor(conf)
devices.append(new_device)

add_devices(devices)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/climate/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMThermostat(hass, conf)
new_device.link_homematic()
new_device = HMThermostat(conf)
devices.append(new_device)

add_devices(devices)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/cover/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMCover(hass, conf)
new_device.link_homematic()
new_device = HMCover(conf)
devices.append(new_device)

add_devices(devices)
Expand Down
57 changes: 29 additions & 28 deletions homeassistant/components/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/homematic/
"""
import asyncio
import os
import time
import logging
from datetime import timedelta
from functools import partial
Expand All @@ -18,7 +18,7 @@
CONF_PLATFORM, CONF_HOSTS, CONF_NAME, ATTR_ENTITY_ID)
from homeassistant.helpers import discovery
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.config import load_yaml_config_file

REQUIREMENTS = ['pyhomematic==0.1.30']
Expand Down Expand Up @@ -121,7 +121,6 @@
]

DATA_HOMEMATIC = 'homematic'
DATA_DELAY = 'homematic_delay'
DATA_DEVINIT = 'homematic_devinit'
DATA_STORE = 'homematic_store'

Expand All @@ -134,7 +133,6 @@
CONF_RESOLVENAMES = 'resolvenames'
CONF_VARIABLES = 'variables'
CONF_DEVICES = 'devices'
CONF_DELAY = 'delay'
CONF_PRIMARY = 'primary'

DEFAULT_LOCAL_IP = '0.0.0.0'
Expand All @@ -145,7 +143,6 @@
DEFAULT_PASSWORD = ''
DEFAULT_VARIABLES = False
DEFAULT_DEVICES = True
DEFAULT_DELAY = 0.5
DEFAULT_PRIMARY = False


Expand Down Expand Up @@ -177,7 +174,6 @@
}},
vol.Optional(CONF_LOCAL_IP, default=DEFAULT_LOCAL_IP): cv.string,
vol.Optional(CONF_LOCAL_PORT, default=DEFAULT_LOCAL_PORT): cv.port,
vol.Optional(CONF_DELAY, default=DEFAULT_DELAY): vol.Coerce(float),
}),
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -249,7 +245,6 @@ def setup(hass, config):
"""Set up the Homematic component."""
from pyhomematic import HMConnection

hass.data[DATA_DELAY] = config[DOMAIN].get(CONF_DELAY)
hass.data[DATA_DEVINIT] = {}
hass.data[DATA_STORE] = set()

Expand Down Expand Up @@ -277,7 +272,7 @@ def setup(hass, config):

# Create server thread
bound_system_callback = partial(_system_callback_handler, hass, config)
hass.data[DATA_HOMEMATIC] = HMConnection(
hass.data[DATA_HOMEMATIC] = homematic = HMConnection(
local=config[DOMAIN].get(CONF_LOCAL_IP),
localport=config[DOMAIN].get(CONF_LOCAL_PORT),
remotes=remotes,
Expand All @@ -286,7 +281,7 @@ def setup(hass, config):
)

# Start server thread, connect to hosts, initialize to receive events
hass.data[DATA_HOMEMATIC].start()
homematic.start()

# Stops server when HASS is shutting down
hass.bus.listen_once(
Expand All @@ -296,7 +291,7 @@ def setup(hass, config):
entity_hubs = []
for _, hub_data in hosts.items():
entity_hubs.append(HMHub(
hass, hub_data[CONF_NAME], hub_data[CONF_VARIABLES]))
homematic, hub_data[CONF_NAME], hub_data[CONF_VARIABLES]))

# Register HomeMatic services
descriptions = load_yaml_config_file(
Expand Down Expand Up @@ -359,7 +354,7 @@ def _service_handle_value(service):

def _service_handle_reconnect(service):
"""Service to reconnect all HomeMatic hubs."""
hass.data[DATA_HOMEMATIC].reconnect()
homematic.reconnect()

hass.services.register(
DOMAIN, SERVICE_RECONNECT, _service_handle_reconnect,
Expand Down Expand Up @@ -575,24 +570,27 @@ def _device_from_servicecall(hass, service):
class HMHub(Entity):
"""The HomeMatic hub. (CCU2/HomeGear)."""

def __init__(self, hass, name, use_variables):
def __init__(self, homematic, name, use_variables):
"""Initialize HomeMatic hub."""
self.hass = hass
self.entity_id = "{}.{}".format(DOMAIN, name.lower())
self._homematic = hass.data[DATA_HOMEMATIC]
self._homematic = homematic
self._variables = {}
self._name = name
self._state = STATE_UNKNOWN
self._use_variables = use_variables

@asyncio.coroutine
def async_added_to_hass(self):
"""Load data init callbacks."""
# Load data
track_time_interval(hass, self._update_hub, SCAN_INTERVAL_HUB)
self._update_hub(None)
async_track_time_interval(
self.hass, self._update_hub, SCAN_INTERVAL_HUB)
yield from self.hass.async_add_job(self._update_hub, None)

if self._use_variables:
track_time_interval(
hass, self._update_variables, SCAN_INTERVAL_VARIABLES)
self._update_variables(None)
async_track_time_interval(
self.hass, self._update_variables, SCAN_INTERVAL_VARIABLES)
yield from self.hass.async_add_job(self._update_variables, None)

@property
def name(self):
Expand Down Expand Up @@ -624,7 +622,9 @@ def _update_hub(self, now):
"""Retrieve latest state."""
state = self._homematic.getServiceMessages(self._name)
self._state = STATE_UNKNOWN if state is None else len(state)
self.schedule_update_ha_state()

if now:
self.schedule_update_ha_state()

def _update_variables(self, now):
"""Retrive all variable data and update hmvariable states."""
Expand All @@ -640,7 +640,7 @@ def _update_variables(self, now):
state_change = True
self._variables.update({key: value})

if state_change:
if state_change and now:
self.schedule_update_ha_state()

def hm_set_variable(self, name, value):
Expand All @@ -662,16 +662,15 @@ def hm_set_variable(self, name, value):
class HMDevice(Entity):
"""The HomeMatic device base object."""

def __init__(self, hass, config):
def __init__(self, config):
"""Initialize a generic HomeMatic device."""
self.hass = hass
self._homematic = hass.data[DATA_HOMEMATIC]
self._name = config.get(ATTR_NAME)
self._address = config.get(ATTR_ADDRESS)
self._proxy = config.get(ATTR_PROXY)
self._channel = config.get(ATTR_CHANNEL)
self._state = config.get(ATTR_PARAM)
self._data = {}
self._homematic = None
self._hmdevice = None
self._connected = False
self._available = False
Expand All @@ -680,6 +679,11 @@ def __init__(self, hass, config):
if self._state:
self._state = self._state.upper()

@asyncio.coroutine
def async_added_to_hass(self):
"""Load data init callbacks."""
yield from self.hass.async_add_job(self.link_homematic)

@property
def should_poll(self):
"""Return false. HomeMatic states are pushed by the XML-RPC Server."""
Expand Down Expand Up @@ -728,16 +732,13 @@ def link_homematic(self):
return True

# Initialize
self._homematic = self.hass.data[DATA_HOMEMATIC]
self._hmdevice = self._homematic.devices[self._proxy][self._address]
self._connected = True

try:
# Initialize datapoints of this object
self._init_data()
if self.hass.data[DATA_DELAY]:
# We optionally delay / pause loading of data to avoid
# overloading of CCU / Homegear
time.sleep(self.hass.data[DATA_DELAY])
self._load_data_from_hm()

# Link events from pyhomematic
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/light/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMLight(hass, conf)
new_device.link_homematic()
new_device = HMLight(conf)
devices.append(new_device)

add_devices(devices)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/sensor/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMSensor(hass, conf)
new_device.link_homematic()
new_device = HMSensor(conf)
devices.append(new_device)

add_devices(devices)
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/switch/homematic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):

devices = []
for conf in discovery_info[ATTR_DISCOVER_DEVICES]:
new_device = HMSwitch(hass, conf)
new_device.link_homematic()
new_device = HMSwitch(conf)
devices.append(new_device)

add_devices(devices)
Expand Down