Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homematic Bugfix: change update handling with variable for breack CCU2 #3215

Merged
merged 1 commit into from Sep 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 41 additions & 11 deletions homeassistant/components/homematic.py
Expand Up @@ -109,6 +109,7 @@
CONF_REMOTE_PORT = 'remote_port'
CONF_RESOLVENAMES = 'resolvenames'
CONF_DELAY = 'delay'
CONF_VARIABLES = 'variables'


DEVICE_SCHEMA = vol.Schema({
Expand All @@ -130,6 +131,7 @@
vol.Optional(CONF_USERNAME, default="Admin"): cv.string,
vol.Optional(CONF_PASSWORD, default=""): cv.string,
vol.Optional(CONF_DELAY, default=0.5): vol.Coerce(float),
vol.Optional(CONF_VARIABLES, default=False): cv.boolean,
}),
}, extra=vol.ALLOW_EXTRA)

Expand Down Expand Up @@ -182,6 +184,7 @@ def setup(hass, config):
username = config[DOMAIN].get(CONF_USERNAME)
password = config[DOMAIN].get(CONF_PASSWORD)
HOMEMATIC_LINK_DELAY = config[DOMAIN].get(CONF_DELAY)
use_variables = config[DOMAIN].get(CONF_VARIABLES)

if remote_ip is None or local_ip is None:
_LOGGER.error("Missing remote CCU/Homegear or local address")
Expand Down Expand Up @@ -219,13 +222,16 @@ def setup(hass, config):

##
# init HM variable
variables = HOMEMATIC.getAllSystemVariables()
variables = HOMEMATIC.getAllSystemVariables() if use_variables else {}
hm_var_store = {}
if variables is not None:
for key, value in variables.items():
entities.append(HMVariable(key, value))
varia = HMVariable(key, value)
hm_var_store.update({key: varia})
entities.append(varia)

# add homematic entites
entities.append(HMHub())
entities.append(HMHub(hm_var_store, use_variables))
component.add_entities(entities)

##
Expand Down Expand Up @@ -496,9 +502,13 @@ def _hm_service_virtualkey(call):
class HMHub(Entity):
"""The Homematic hub. I.e. CCU2/HomeGear."""

def __init__(self):
def __init__(self, variables_store, use_variables=False):
"""Initialize Homematic hub."""
self._state = STATE_UNKNOWN
self._store = variables_store
self._use_variables = use_variables

self.update()

@property
def name(self):
Expand All @@ -525,14 +535,30 @@ def available(self):
"""Return true if device is available."""
return True if HOMEMATIC is not None else False

@Throttle(MIN_TIME_BETWEEN_UPDATE_HUB)
def update(self):
"""Update Hub data and all HM variables."""
self._update_hub_state()
self._update_variables_state()

@Throttle(MIN_TIME_BETWEEN_UPDATE_HUB)
def _update_hub_state(self):
"""Retrieve latest state."""
if HOMEMATIC is None:
return
state = HOMEMATIC.getServiceMessages()
self._state = STATE_UNKNOWN if state is None else len(state)

@Throttle(MIN_TIME_BETWEEN_UPDATE_VAR)
def _update_variables_state(self):
"""Retrive all variable data and update hmvariable states."""
if HOMEMATIC is None or not self._use_variables:
return
variables = HOMEMATIC.getAllSystemVariables()
if variables is not None:
for key, value in variables.items():
if key in self._store:
self._store.get(key).hm_update(value)


class HMVariable(Entity):
"""The Homematic system variable."""
Expand All @@ -557,12 +583,16 @@ def icon(self):
"""Return the icon to use in the frontend, if any."""
return "mdi:code-string"

@Throttle(MIN_TIME_BETWEEN_UPDATE_VAR)
def update(self):
"""Retrieve latest state."""
if HOMEMATIC is None:
return
self._state = HOMEMATIC.getSystemVariable(self._name)
@property
def should_poll(self):
"""Return false. Homematic Hub object update variable."""
return False

def hm_update(self, value):
"""Update variable over Hub object."""
if value != self._state:
self._state = value
self.update_ha_state()

def hm_set(self, value):
"""Set variable on homematic controller."""
Expand Down