Merge branch 'pep257-scene-mqtt' into dev

This commit is contained in:
Fabian Affolter 2016-03-08 08:11:30 +01:00
commit 5423252a52
7 changed files with 73 additions and 75 deletions

View file

@ -1,7 +1,5 @@
""" """
homeassistant.components.rollershutter Support for Roller shutters.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Rollershutter component.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/rollershutter/ https://home-assistant.io/components/rollershutter/
@ -36,38 +34,38 @@ ATTR_CURRENT_POSITION = 'current_position'
def is_open(hass, entity_id=None): def is_open(hass, entity_id=None):
""" Returns if the rollershutter is open based on the statemachine. """ """Return if the roller shutter is open based on the statemachine."""
entity_id = entity_id or ENTITY_ID_ALL_ROLLERSHUTTERS entity_id = entity_id or ENTITY_ID_ALL_ROLLERSHUTTERS
return hass.states.is_state(entity_id, STATE_OPEN) return hass.states.is_state(entity_id, STATE_OPEN)
def move_up(hass, entity_id=None): def move_up(hass, entity_id=None):
""" Move up all or specified rollershutter. """ """Move up all or specified roller shutter."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_MOVE_UP, data) hass.services.call(DOMAIN, SERVICE_MOVE_UP, data)
def move_down(hass, entity_id=None): def move_down(hass, entity_id=None):
""" Move down all or specified rollershutter. """ """Move down all or specified roller shutter."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_MOVE_DOWN, data) hass.services.call(DOMAIN, SERVICE_MOVE_DOWN, data)
def stop(hass, entity_id=None): def stop(hass, entity_id=None):
""" Stops all or specified rollershutter. """ """Stop all or specified roller shutter."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_STOP, data) hass.services.call(DOMAIN, SERVICE_STOP, data)
def setup(hass, config): def setup(hass, config):
""" Track states and offer events for rollershutters. """ """Track states and offer events for roller shutters."""
component = EntityComponent( component = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, DISCOVERY_PLATFORMS, _LOGGER, DOMAIN, hass, SCAN_INTERVAL, DISCOVERY_PLATFORMS,
GROUP_NAME_ALL_ROLLERSHUTTERS) GROUP_NAME_ALL_ROLLERSHUTTERS)
component.setup(config) component.setup(config)
def handle_rollershutter_service(service): def handle_rollershutter_service(service):
""" Handles calls to the rollershutter services. """ """Handle calls to the roller shutter services."""
target_rollershutters = component.extract_from_service(service) target_rollershutters = component.extract_from_service(service)
for rollershutter in target_rollershutters: for rollershutter in target_rollershutters:
@ -98,20 +96,20 @@ def setup(hass, config):
class RollershutterDevice(Entity): class RollershutterDevice(Entity):
""" Represents a rollershutter within Home Assistant. """ """Representation a rollers hutter."""
# pylint: disable=no-self-use
# pylint: disable=no-self-use
@property @property
def current_position(self): def current_position(self):
""" """Return current position of roller shutter.
Return current position of rollershutter.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
raise NotImplementedError() raise NotImplementedError()
@property @property
def state(self): def state(self):
""" Returns the state of the rollershutter. """ """Return the state of the roller shutter."""
current = self.current_position current = self.current_position
if current is None: if current is None:
@ -121,7 +119,7 @@ class RollershutterDevice(Entity):
@property @property
def state_attributes(self): def state_attributes(self):
""" Return the state attributes. """ """Return the state attributes."""
current = self.current_position current = self.current_position
if current is None: if current is None:
@ -132,13 +130,13 @@ class RollershutterDevice(Entity):
} }
def move_up(self, **kwargs): def move_up(self, **kwargs):
""" Move the rollershutter down. """ """Move the roller shutter down."""
raise NotImplementedError() raise NotImplementedError()
def move_down(self, **kwargs): def move_down(self, **kwargs):
""" Move the rollershutter up. """ """Move the roller shutter up."""
raise NotImplementedError() raise NotImplementedError()
def stop(self, **kwargs): def stop(self, **kwargs):
""" Stop the rollershutter. """ """Stop the roller shutter."""
raise NotImplementedError() raise NotImplementedError()

View file

@ -15,8 +15,7 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup rollershutter controlled by shell commands.""" """Setup roller shutter controlled by shell commands."""
rollershutters = config.get('rollershutters', {}) rollershutters = config.get('rollershutters', {})
devices = [] devices = []
@ -35,15 +34,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# pylint: disable=too-many-arguments, too-many-instance-attributes # pylint: disable=too-many-arguments, too-many-instance-attributes
class CommandRollershutter(RollershutterDevice): class CommandRollershutter(RollershutterDevice):
""" Represents a rollershutter - can be controlled using shell cmd. """ """Representation a command line roller shutter."""
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, hass, name, command_up, command_down, command_stop, def __init__(self, hass, name, command_up, command_down, command_stop,
command_state, value_template): command_state, value_template):
"""Initialize the roller shutter."""
self._hass = hass self._hass = hass
self._name = name self._name = name
self._state = None # Unknown self._state = None
self._command_up = command_up self._command_up = command_up
self._command_down = command_down self._command_down = command_down
self._command_stop = command_stop self._command_stop = command_stop
@ -52,7 +51,7 @@ class CommandRollershutter(RollershutterDevice):
@staticmethod @staticmethod
def _move_rollershutter(command): def _move_rollershutter(command):
""" Execute the actual commands. """ """Execute the actual commands."""
_LOGGER.info('Running command: %s', command) _LOGGER.info('Running command: %s', command)
success = (subprocess.call(command, shell=True) == 0) success = (subprocess.call(command, shell=True) == 0)
@ -64,7 +63,7 @@ class CommandRollershutter(RollershutterDevice):
@staticmethod @staticmethod
def _query_state_value(command): def _query_state_value(command):
""" Execute state command for return value. """ """Execute state command for return value."""
_LOGGER.info('Running state command: %s', command) _LOGGER.info('Running state command: %s', command)
try: try:
@ -75,31 +74,31 @@ class CommandRollershutter(RollershutterDevice):
@property @property
def should_poll(self): def should_poll(self):
""" Only poll if we have statecmd. """ """Only poll if we have state command."""
return self._command_state is not None return self._command_state is not None
@property @property
def name(self): def name(self):
""" The name of the rollershutter. """ """Return the name of the roller shutter."""
return self._name return self._name
@property @property
def current_position(self): def current_position(self):
""" """Return current position of roller shutter.
Return current position of rollershutter.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
return self._state return self._state
def _query_state(self): def _query_state(self):
""" Query for state. """ """Query for the state."""
if not self._command_state: if not self._command_state:
_LOGGER.error('No state command specified') _LOGGER.error('No state command specified')
return return
return self._query_state_value(self._command_state) return self._query_state_value(self._command_state)
def update(self): def update(self):
""" Update device state. """ """Update device state."""
if self._command_state: if self._command_state:
payload = str(self._query_state()) payload = str(self._query_state())
if self._value_template: if self._value_template:
@ -108,13 +107,13 @@ class CommandRollershutter(RollershutterDevice):
self._state = int(payload) self._state = int(payload)
def move_up(self, **kwargs): def move_up(self, **kwargs):
""" Move the rollershutter up. """ """Move the roller shutter up."""
self._move_rollershutter(self._command_up) self._move_rollershutter(self._command_up)
def move_down(self, **kwargs): def move_down(self, **kwargs):
""" Move the rollershutter down. """ """Move the roller shutter down."""
self._move_rollershutter(self._command_down) self._move_rollershutter(self._command_down)
def stop(self, **kwargs): def stop(self, **kwargs):
""" Stop the device. """ """Stop the device."""
self._move_rollershutter(self._command_stop) self._move_rollershutter(self._command_stop)

View file

@ -18,9 +18,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class DemoRollershutter(RollershutterDevice): class DemoRollershutter(RollershutterDevice):
"""Represents a roller shutter.""" """Representation of a demo roller shutter."""
# pylint: disable=no-self-use # pylint: disable=no-self-use
def __init__(self, hass, name, position): def __init__(self, hass, name, position):
"""Initialize the roller shutter."""
self.hass = hass self.hass = hass
self._name = name self._name = name
self._position = position self._position = position
@ -29,7 +31,7 @@ class DemoRollershutter(RollershutterDevice):
@property @property
def name(self): def name(self):
"""Returns the name of the roller shutter.""" """Return the name of the roller shutter."""
return self._name return self._name
@property @property
@ -59,7 +61,7 @@ class DemoRollershutter(RollershutterDevice):
self._moving_up = False self._moving_up = False
def stop(self, **kwargs): def stop(self, **kwargs):
"""Stops the roller shutter.""" """Stop the roller shutter."""
if self._listener is not None: if self._listener is not None:
self.hass.bus.remove_listener(EVENT_TIME_CHANGED, self._listener) self.hass.bus.remove_listener(EVENT_TIME_CHANGED, self._listener)
self._listener = None self._listener = None

View file

@ -24,7 +24,6 @@ DEFAULT_PAYLOAD_STOP = "STOP"
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Add MQTT Rollershutter.""" """Add MQTT Rollershutter."""
if config.get('command_topic') is None: if config.get('command_topic') is None:
_LOGGER.error("Missing required variable: command_topic") _LOGGER.error("Missing required variable: command_topic")
return False return False
@ -43,9 +42,11 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# pylint: disable=too-many-arguments, too-many-instance-attributes # pylint: disable=too-many-arguments, too-many-instance-attributes
class MqttRollershutter(RollershutterDevice): class MqttRollershutter(RollershutterDevice):
"""Represents a rollershutter that can be controlled using MQTT.""" """Representation of a roller shutter that can be controlled using MQTT."""
def __init__(self, hass, name, state_topic, command_topic, qos, def __init__(self, hass, name, state_topic, command_topic, qos,
payload_up, payload_down, payload_stop, value_template): payload_up, payload_down, payload_stop, value_template):
"""Initialize the roller shutter."""
self._state = None self._state = None
self._hass = hass self._hass = hass
self._name = name self._name = name
@ -80,24 +81,24 @@ class MqttRollershutter(RollershutterDevice):
@property @property
def name(self): def name(self):
"""The name of the rollershutter.""" """Return the name of the roller shutter."""
return self._name return self._name
@property @property
def current_position(self): def current_position(self):
""" """Return current position of roller shutter.
Return current position of rollershutter.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
return self._state return self._state
def move_up(self, **kwargs): def move_up(self, **kwargs):
"""Move the rollershutter up.""" """Move the roller shutter up."""
mqtt.publish(self.hass, self._command_topic, self._payload_up, mqtt.publish(self.hass, self._command_topic, self._payload_up,
self._qos) self._qos)
def move_down(self, **kwargs): def move_down(self, **kwargs):
"""Move the rollershutter down.""" """Move the roller shutter down."""
mqtt.publish(self.hass, self._command_topic, self._payload_down, mqtt.publish(self.hass, self._command_topic, self._payload_down,
self._qos) self._qos)

View file

@ -1,7 +1,5 @@
""" """
homeassistant.components.rollershutter.scsgate Allow to configure a SCSGate roller shutter.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allows to configure a SCSGate rollershutter.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/rollershutter.scsgate/ https://home-assistant.io/components/rollershutter.scsgate/
@ -15,8 +13,7 @@ DEPENDENCIES = ['scsgate']
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Add the SCSGate swiches defined inside of the configuration file. """ """Setup the SCSGate roller shutter."""
devices = config.get('devices') devices = config.get('devices')
rollershutters = [] rollershutters = []
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -42,57 +39,59 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# pylint: disable=too-many-arguments, too-many-instance-attributes # pylint: disable=too-many-arguments, too-many-instance-attributes
class SCSGateRollerShutter(RollershutterDevice): class SCSGateRollerShutter(RollershutterDevice):
""" Represents a rollershutter that can be controlled using SCSGate. """ """Representation of SCSGate rollershutter."""
def __init__(self, scs_id, name, logger): def __init__(self, scs_id, name, logger):
"""Initialize the roller shutter."""
self._scs_id = scs_id self._scs_id = scs_id
self._name = name self._name = name
self._logger = logger self._logger = logger
@property @property
def scs_id(self): def scs_id(self):
""" SCSGate ID """ """Return the SCSGate ID."""
return self._scs_id return self._scs_id
@property @property
def should_poll(self): def should_poll(self):
""" No polling needed """ """No polling needed."""
return False return False
@property @property
def name(self): def name(self):
""" The name of the rollershutter. """ """Return the name of the roller shutter."""
return self._name return self._name
@property @property
def current_position(self): def current_position(self):
""" """Return current position of roller shutter.
Return current position of rollershutter.
None is unknown, 0 is closed, 100 is fully open. None is unknown, 0 is closed, 100 is fully open.
""" """
return None return None
def move_up(self, **kwargs): def move_up(self, **kwargs):
""" Move the rollershutter up. """ """Move the roller shutter up."""
from scsgate.tasks import RaiseRollerShutterTask from scsgate.tasks import RaiseRollerShutterTask
scsgate.SCSGATE.append_task( scsgate.SCSGATE.append_task(
RaiseRollerShutterTask(target=self._scs_id)) RaiseRollerShutterTask(target=self._scs_id))
def move_down(self, **kwargs): def move_down(self, **kwargs):
""" Move the rollershutter down. """ """Move the rollers hutter down."""
from scsgate.tasks import LowerRollerShutterTask from scsgate.tasks import LowerRollerShutterTask
scsgate.SCSGATE.append_task( scsgate.SCSGATE.append_task(
LowerRollerShutterTask(target=self._scs_id)) LowerRollerShutterTask(target=self._scs_id))
def stop(self, **kwargs): def stop(self, **kwargs):
""" Stop the device. """ """Stop the device."""
from scsgate.tasks import HaltRollerShutterTask from scsgate.tasks import HaltRollerShutterTask
scsgate.SCSGATE.append_task(HaltRollerShutterTask(target=self._scs_id)) scsgate.SCSGATE.append_task(HaltRollerShutterTask(target=self._scs_id))
def process_event(self, message): def process_event(self, message):
""" Handle a SCSGate message related with this rollershutter """ """Handle a SCSGate message related with this roller shutter."""
self._logger.debug( self._logger.debug(
"Rollershutter %s, got message %s", "Rollershutter %s, got message %s",
self._scs_id, message.toggled) self._scs_id, message.toggled)

View file

@ -1,7 +1,5 @@
""" """
homeassistant.components.scene Allow users to set and activate scenes.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allows users to set and activate scenes.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/scene/ https://home-assistant.io/components/scene/
@ -25,7 +23,7 @@ SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
def activate(hass, entity_id=None): def activate(hass, entity_id=None):
""" Activate a scene. """ """Activate a scene."""
data = {} data = {}
if entity_id: if entity_id:
@ -35,8 +33,7 @@ def activate(hass, entity_id=None):
def setup(hass, config): def setup(hass, config):
""" Sets up scenes. """ """Setup the scenes."""
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# You are not allowed to mutate the original config so make a copy # You are not allowed to mutate the original config so make a copy
@ -58,7 +55,7 @@ def setup(hass, config):
component.setup(config) component.setup(config)
def handle_scene_service(service): def handle_scene_service(service):
""" Handles calls to the switch services. """ """Handle calls to the switch services."""
target_scenes = component.extract_from_service(service) target_scenes = component.extract_from_service(service)
for scene in target_scenes: for scene in target_scenes:
@ -70,16 +67,18 @@ def setup(hass, config):
class Scene(Entity): class Scene(Entity):
""" A scene is a group of entities and the states we want them to be. """ """A scene is a group of entities and the states we want them to be."""
@property @property
def should_poll(self): def should_poll(self):
"""No polling needed."""
return False return False
@property @property
def state(self): def state(self):
"""Return the state."""
return STATE return STATE
def activate(self): def activate(self):
""" Activates scene. Tries to get entities into requested state. """ """Activate scene. Tries to get entities into requested state."""
raise NotImplementedError raise NotImplementedError

View file

@ -1,6 +1,4 @@
""" """
homeassistant.components.scene
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allows users to set and activate scenes. Allows users to set and activate scenes.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
@ -24,7 +22,7 @@ SceneConfig = namedtuple('SceneConfig', ['name', 'states'])
# pylint: disable=unused-argument # pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up home assistant scene entries. """ """Setup home assistant scene entries."""
scene_config = config.get("states") scene_config = config.get("states")
if not isinstance(scene_config, list): if not isinstance(scene_config, list):
@ -37,7 +35,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
def _process_config(scene_config): def _process_config(scene_config):
""" Process passed in config into a format to work with. """ """Process passed in config into a format to work with."""
name = scene_config.get('name') name = scene_config.get('name')
states = {} states = {}
@ -65,23 +63,25 @@ def _process_config(scene_config):
class HomeAssistantScene(Scene): class HomeAssistantScene(Scene):
""" A scene is a group of entities and the states we want them to be. """ """A scene is a group of entities and the states we want them to be."""
def __init__(self, hass, scene_config): def __init__(self, hass, scene_config):
"""Initialize the scene."""
self.hass = hass self.hass = hass
self.scene_config = scene_config self.scene_config = scene_config
@property @property
def name(self): def name(self):
"""Return the name of the scene."""
return self.scene_config.name return self.scene_config.name
@property @property
def device_state_attributes(self): def device_state_attributes(self):
""" Scene state attributes. """ """Return the scene state attributes."""
return { return {
ATTR_ENTITY_ID: list(self.scene_config.states.keys()), ATTR_ENTITY_ID: list(self.scene_config.states.keys()),
} }
def activate(self): def activate(self):
""" Activates scene. Tries to get entities into requested state. """ """Activate scene. Tries to get entities into requested state."""
reproduce_state(self.hass, self.scene_config.states.values(), True) reproduce_state(self.hass, self.scene_config.states.values(), True)