change update handling with variable for breack CCU2 (#3215)

pull/3217/head
Pascal Vizeli 2016-09-05 22:39:29 +02:00 committed by GitHub
parent 17a2cac7e1
commit 73036f4725
1 changed files with 41 additions and 11 deletions

View File

@ -109,6 +109,7 @@ CONF_REMOTE_IP = 'remote_ip'
CONF_REMOTE_PORT = 'remote_port' CONF_REMOTE_PORT = 'remote_port'
CONF_RESOLVENAMES = 'resolvenames' CONF_RESOLVENAMES = 'resolvenames'
CONF_DELAY = 'delay' CONF_DELAY = 'delay'
CONF_VARIABLES = 'variables'
DEVICE_SCHEMA = vol.Schema({ DEVICE_SCHEMA = vol.Schema({
@ -130,6 +131,7 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_USERNAME, default="Admin"): cv.string, vol.Optional(CONF_USERNAME, default="Admin"): cv.string,
vol.Optional(CONF_PASSWORD, default=""): cv.string, vol.Optional(CONF_PASSWORD, default=""): cv.string,
vol.Optional(CONF_DELAY, default=0.5): vol.Coerce(float), vol.Optional(CONF_DELAY, default=0.5): vol.Coerce(float),
vol.Optional(CONF_VARIABLES, default=False): cv.boolean,
}), }),
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
@ -182,6 +184,7 @@ def setup(hass, config):
username = config[DOMAIN].get(CONF_USERNAME) username = config[DOMAIN].get(CONF_USERNAME)
password = config[DOMAIN].get(CONF_PASSWORD) password = config[DOMAIN].get(CONF_PASSWORD)
HOMEMATIC_LINK_DELAY = config[DOMAIN].get(CONF_DELAY) 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: if remote_ip is None or local_ip is None:
_LOGGER.error("Missing remote CCU/Homegear or local address") _LOGGER.error("Missing remote CCU/Homegear or local address")
@ -219,13 +222,16 @@ def setup(hass, config):
## ##
# init HM variable # init HM variable
variables = HOMEMATIC.getAllSystemVariables() variables = HOMEMATIC.getAllSystemVariables() if use_variables else {}
hm_var_store = {}
if variables is not None: if variables is not None:
for key, value in variables.items(): 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 # add homematic entites
entities.append(HMHub()) entities.append(HMHub(hm_var_store, use_variables))
component.add_entities(entities) component.add_entities(entities)
## ##
@ -496,9 +502,13 @@ def _hm_service_virtualkey(call):
class HMHub(Entity): class HMHub(Entity):
"""The Homematic hub. I.e. CCU2/HomeGear.""" """The Homematic hub. I.e. CCU2/HomeGear."""
def __init__(self): def __init__(self, variables_store, use_variables=False):
"""Initialize Homematic hub.""" """Initialize Homematic hub."""
self._state = STATE_UNKNOWN self._state = STATE_UNKNOWN
self._store = variables_store
self._use_variables = use_variables
self.update()
@property @property
def name(self): def name(self):
@ -525,14 +535,30 @@ class HMHub(Entity):
"""Return true if device is available.""" """Return true if device is available."""
return True if HOMEMATIC is not None else False return True if HOMEMATIC is not None else False
@Throttle(MIN_TIME_BETWEEN_UPDATE_HUB)
def update(self): 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.""" """Retrieve latest state."""
if HOMEMATIC is None: if HOMEMATIC is None:
return return
state = HOMEMATIC.getServiceMessages() state = HOMEMATIC.getServiceMessages()
self._state = STATE_UNKNOWN if state is None else len(state) 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): class HMVariable(Entity):
"""The Homematic system variable.""" """The Homematic system variable."""
@ -557,12 +583,16 @@ class HMVariable(Entity):
"""Return the icon to use in the frontend, if any.""" """Return the icon to use in the frontend, if any."""
return "mdi:code-string" return "mdi:code-string"
@Throttle(MIN_TIME_BETWEEN_UPDATE_VAR) @property
def update(self): def should_poll(self):
"""Retrieve latest state.""" """Return false. Homematic Hub object update variable."""
if HOMEMATIC is None: return False
return
self._state = HOMEMATIC.getSystemVariable(self._name) 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): def hm_set(self, value):
"""Set variable on homematic controller.""" """Set variable on homematic controller."""