verisure refactoring and fix reconnect

This commit is contained in:
Per Sandström committed 2016-02-27 21:50:19 +01:00
1 parent dc02370b43
commit d7e3c6a442
7 files changed
+213 -219

No files matched your search

+16 -22
View File
@@ -8,7 +8,7 @@ documentation at https://home-assistant.io/components/verisure/
"""
import logging
import homeassistant.components.verisure as verisure
from homeassistant.components.verisure import HUB as hub
from homeassistant.components.switch import SwitchDevice
_LOGGER = logging.getLogger(__name__)
@@ -16,49 +16,43 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Sets up the Verisure platform. """
if not verisure.MY_PAGES:
_LOGGER.error('A connection has not been made to Verisure mypages.')
if not int(hub.config.get('smartplugs', '1')):
return False
hub.update_smartplugs()
switches = []
switches.extend([
VerisureSmartplug(value)
for value in verisure.SMARTPLUG_STATUS.values()
if verisure.SHOW_SMARTPLUGS
])
VerisureSmartplug(value.id)
for value in hub.smartplug_status.values()])
add_devices(switches)
class VerisureSmartplug(SwitchDevice):
""" Represents a Verisure smartplug. """
def __init__(self, smartplug_status):
self._id = smartplug_status.id
def __init__(self, device_id):
self._id = device_id
@property
def name(self):
""" Get the name (location) of the smartplug. """
return verisure.SMARTPLUG_STATUS[self._id].location
return hub.smartplug_status[self._id].location
@property
def is_on(self):
""" Returns True if on """
plug_status = verisure.SMARTPLUG_STATUS[self._id].status
return plug_status == 'on'
return hub.smartplug_status[self._id].status == 'on'
def turn_on(self):
""" Set smartplug status on. """
verisure.MY_PAGES.smartplug.set(self._id, 'on')
verisure.MY_PAGES.smartplug.wait_while_updating(self._id, 'on')
verisure.update_smartplug()
hub.my_pages.smartplug.set(self._id, 'on')
hub.my_pages.smartplug.wait_while_updating(self._id, 'on')
self.update()
def turn_off(self):
""" Set smartplug status off. """
verisure.MY_PAGES.smartplug.set(self._id, 'off')
verisure.MY_PAGES.smartplug.wait_while_updating(self._id, 'off')
verisure.update_smartplug()
hub.my_pages.smartplug.set(self._id, 'off')
hub.my_pages.smartplug.wait_while_updating(self._id, 'off')
self.update()
def update(self):
verisure.update_smartplug()
hub.update_smartplugs()