core/homeassistant/components/rollershutter/demo.py

94 lines
2.6 KiB
Python
Raw Normal View History

2015-12-05 09:12:38 +00:00
"""
Demo platform for the rollor shutter component.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
2015-12-05 09:12:38 +00:00
"""
2016-02-19 05:27:50 +00:00
from homeassistant.components.rollershutter import RollershutterDevice
2015-12-05 09:12:38 +00:00
from homeassistant.const import EVENT_TIME_CHANGED
from homeassistant.helpers.event import track_utc_time_change
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Demo roller shutters."""
2015-12-05 09:12:38 +00:00
add_devices([
DemoRollershutter(hass, 'Kitchen Window', 0),
DemoRollershutter(hass, 'Living Room Window', 100),
])
class DemoRollershutter(RollershutterDevice):
2016-03-07 22:01:34 +00:00
"""Representation of a demo roller shutter."""
2015-12-05 09:12:38 +00:00
# pylint: disable=no-self-use
def __init__(self, hass, name, position):
2016-03-07 22:01:34 +00:00
"""Initialize the roller shutter."""
2015-12-05 09:12:38 +00:00
self.hass = hass
self._name = name
self._position = position
self._moving_up = True
self._listener = None
@property
def name(self):
2016-03-07 22:01:34 +00:00
"""Return the name of the roller shutter."""
2015-12-05 09:12:38 +00:00
return self._name
@property
def should_poll(self):
"""No polling needed for a demo roller shutter."""
2015-12-05 09:12:38 +00:00
return False
@property
def current_position(self):
"""Return the current position of the roller shutter."""
2015-12-05 09:12:38 +00:00
return self._position
def move_up(self, **kwargs):
"""Move the roller shutter down."""
2015-12-05 09:12:38 +00:00
if self._position == 0:
return
self._listen()
self._moving_up = True
def move_down(self, **kwargs):
"""Move the roller shutter up."""
2015-12-05 09:12:38 +00:00
if self._position == 100:
return
self._listen()
self._moving_up = False
def move_position(self, position, **kwargs):
"""Move the roller shutter to a specific position."""
if self._position == position:
return
self._listen()
self._moving_up = position < self._position
2015-12-05 09:12:38 +00:00
def stop(self, **kwargs):
2016-03-07 22:01:34 +00:00
"""Stop the roller shutter."""
2015-12-05 09:12:38 +00:00
if self._listener is not None:
self.hass.bus.remove_listener(EVENT_TIME_CHANGED, self._listener)
self._listener = None
def _listen(self):
"""Listen for changes."""
2015-12-05 09:12:38 +00:00
if self._listener is None:
self._listener = track_utc_time_change(self.hass,
self._time_changed)
def _time_changed(self, now):
"""Track time changes."""
2015-12-05 09:12:38 +00:00
if self._moving_up:
self._position -= 10
else:
self._position += 10
if self._position % 100 == 0:
self.stop()
self.update_ha_state()