Devices can now be polling or push

This commit is contained in:
Paulus Schoutsen 2015-02-28 22:33:44 -08:00
parent 8567dd001b
commit 8bd803601f
5 changed files with 38 additions and 12 deletions

View File

@ -178,12 +178,14 @@ def setup(hass, config):
""" Add lights to the component to track. """ """ Add lights to the component to track. """
for light in new_lights: for light in new_lights:
if light is not None and light not in lights.values(): if light is not None and light not in lights.values():
light.hass = hass
light.entity_id = generate_entity_id( light.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, light.name, lights.keys()) ENTITY_ID_FORMAT, light.name, lights.keys())
lights[light.entity_id] = light lights[light.entity_id] = light
light.update_ha_state(hass) light.update_ha_state()
light_group.update_tracked_entity_ids(lights.keys()) light_group.update_tracked_entity_ids(lights.keys())
@ -201,7 +203,8 @@ def setup(hass, config):
_LOGGER.info("Updating light states") _LOGGER.info("Updating light states")
for light in lights.values(): for light in lights.values():
light.update_ha_state(hass, True) if light.should_poll:
light.update_ha_state(True)
update_lights_state(None) update_lights_state(None)
@ -298,7 +301,7 @@ def setup(hass, config):
light.turn_on(**params) light.turn_on(**params)
for light in target_lights: for light in target_lights:
light.update_ha_state(hass, True) light.update_ha_state(True)
# Update light state every 30 seconds # Update light state every 30 seconds
hass.track_time_change(update_lights_state, second=[0, 30]) hass.track_time_change(update_lights_state, second=[0, 30])

View File

@ -40,7 +40,8 @@ def setup(hass, config):
""" Update states of all sensors. """ """ Update states of all sensors. """
if sensors: if sensors:
for sensor in sensors.values(): for sensor in sensors.values():
sensor.update_ha_state(hass, True) if sensor.should_poll:
sensor.update_ha_state(True)
update_sensor_states(None) update_sensor_states(None)
@ -53,12 +54,14 @@ def setup(hass, config):
for sensor in discovered: for sensor in discovered:
if sensor is not None and sensor not in sensors.values(): if sensor is not None and sensor not in sensors.values():
sensor.hass = hass
sensor.entity_id = generate_entity_id( sensor.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, sensor.name, sensors.keys()) ENTITY_ID_FORMAT, sensor.name, sensors.keys())
sensors[sensor.entity_id] = sensor sensors[sensor.entity_id] = sensor
sensor.update_ha_state(hass) sensor.update_ha_state()
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), sensor_discovered) discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), sensor_discovered)

View File

@ -71,7 +71,7 @@ def setup(hass, config):
logger.info("Updating switch states") logger.info("Updating switch states")
for switch in switches.values(): for switch in switches.values():
switch.update_ha_state(hass, True) switch.update_ha_state(True)
update_states(None) update_states(None)
@ -88,12 +88,14 @@ def setup(hass, config):
for switch in discovered: for switch in discovered:
if switch is not None and switch not in switches.values(): if switch is not None and switch not in switches.values():
switch.hass = hass
switch.entity_id = generate_entity_id( switch.entity_id = generate_entity_id(
ENTITY_ID_FORMAT, switch.name, switches.keys()) ENTITY_ID_FORMAT, switch.name, switches.keys())
switches[switch.entity_id] = switch switches[switch.entity_id] = switch
switch.update_ha_state(hass) switch.update_ha_state()
switch_group.update_tracked_entity_ids(switches.keys()) switch_group.update_tracked_entity_ids(switches.keys())
@ -114,7 +116,7 @@ def setup(hass, config):
else: else:
switch.turn_off() switch.turn_off()
switch.update_ha_state(hass) switch.update_ha_state(True)
# Update state every 30 seconds # Update state every 30 seconds
hass.track_time_change(update_states, second=[0, 30]) hass.track_time_change(update_states, second=[0, 30])

View File

@ -82,7 +82,8 @@ def setup(hass, config):
logger.info("Updating thermostat state") logger.info("Updating thermostat state")
for thermostat in thermostats.values(): for thermostat in thermostats.values():
thermostat.update_ha_state(hass, True) if thermostat.should_poll:
thermostat.update_ha_state(True)
# Update state every minute # Update state every minute
hass.track_time_change(update_state, second=[0]) hass.track_time_change(update_state, second=[0])
@ -125,7 +126,7 @@ def setup(hass, config):
thermostat.set_temperature(temperature) thermostat.set_temperature(temperature)
for thermostat in target_thermostats: for thermostat in target_thermostats:
thermostat.update_ha_state(hass, True) thermostat.update_ha_state(True)
hass.services.register( hass.services.register(
DOMAIN, SERVICE_SET_AWAY_MODE, thermostat_service) DOMAIN, SERVICE_SET_AWAY_MODE, thermostat_service)

View File

@ -159,6 +159,8 @@ def platform_devices_from_config(config, domain, hass,
no_name_count = 0 no_name_count = 0
for device in devices: for device in devices:
device.hass = hass
# Get the name or set to default if none given # Get the name or set to default if none given
name = device.name or DEVICE_DEFAULT_NAME name = device.name or DEVICE_DEFAULT_NAME
@ -179,8 +181,17 @@ class Device(object):
""" ABC for Home Assistant devices. """ """ ABC for Home Assistant devices. """
# pylint: disable=no-self-use # pylint: disable=no-self-use
hass = None
entity_id = None entity_id = None
@property
def should_poll(self):
"""
Return True if device has to be polled for state.
False if device pushes its state to HA.
"""
return True
@property @property
def unique_id(self): def unique_id(self):
""" Returns a unique id. """ """ Returns a unique id. """
@ -222,11 +233,14 @@ class Device(object):
""" Retrieve latest state from the real device. """ """ Retrieve latest state from the real device. """
pass pass
def update_ha_state(self, hass, force_refresh=False): def update_ha_state(self, force_refresh=False):
""" """
Updates Home Assistant with current state of device. Updates Home Assistant with current state of device.
If force_refresh == True will update device before setting state. If force_refresh == True will update device before setting state.
""" """
if self.hass is None:
raise RuntimeError("Attribute hass is None for {}".format(self))
if self.entity_id is None: if self.entity_id is None:
raise NoEntitySpecifiedError( raise NoEntitySpecifiedError(
"No entity specified for device {}".format(self.name)) "No entity specified for device {}".format(self.name))
@ -239,12 +253,15 @@ class Device(object):
if ATTR_FRIENDLY_NAME not in attr and self.name: if ATTR_FRIENDLY_NAME not in attr and self.name:
attr[ATTR_FRIENDLY_NAME] = self.name attr[ATTR_FRIENDLY_NAME] = self.name
return hass.states.set(self.entity_id, self.state, attr) return self.hass.states.set(self.entity_id, self.state, attr)
def __eq__(self, other): def __eq__(self, other):
return (isinstance(other, Device) and return (isinstance(other, Device) and
other.unique_id == self.unique_id) other.unique_id == self.unique_id)
def __repr__(self):
return "<Device {}: {}>".format(self.name, self.state)
class ToggleDevice(Device): class ToggleDevice(Device):
""" ABC for devices that can be turned on and off. """ """ ABC for devices that can be turned on and off. """