core/homeassistant/components/hive/switch.py

94 lines
2.9 KiB
Python
Raw Normal View History

2018-01-15 22:24:12 +00:00
"""
Support for the Hive devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.hive/
"""
from homeassistant.components.switch import SwitchDevice
2019-01-09 04:15:12 +00:00
from homeassistant.components.hive import DATA_HIVE, DOMAIN
2018-01-15 22:24:12 +00:00
DEPENDENCIES = ['hive']
def setup_platform(hass, config, add_entities, discovery_info=None):
2018-01-15 22:24:12 +00:00
"""Set up Hive switches."""
if discovery_info is None:
return
session = hass.data.get(DATA_HIVE)
add_entities([HiveDevicePlug(session, discovery_info)])
2018-01-15 22:24:12 +00:00
class HiveDevicePlug(SwitchDevice):
"""Hive Active Plug."""
def __init__(self, hivesession, hivedevice):
"""Initialize the Switch device."""
self.node_id = hivedevice["Hive_NodeID"]
self.node_name = hivedevice["Hive_NodeName"]
self.device_type = hivedevice["HA_DeviceType"]
self.session = hivesession
self.attributes = {}
2018-01-15 22:24:12 +00:00
self.data_updatesource = '{}.{}'.format(self.device_type,
self.node_id)
2019-01-09 04:15:12 +00:00
self._unique_id = '{}-{}'.format(self.node_id, self.device_type)
2018-01-15 22:24:12 +00:00
self.session.entities.append(self)
2019-01-09 04:15:12 +00:00
@property
def unique_id(self):
"""Return unique ID of entity."""
return self._unique_id
@property
def device_info(self):
"""Return device information."""
return {
'identifiers': {
(DOMAIN, self.unique_id)
},
'name': self.name
}
2018-01-15 22:24:12 +00:00
def handle_update(self, updatesource):
"""Handle the new update request."""
if '{}.{}'.format(self.device_type, self.node_id) not in updatesource:
self.schedule_update_ha_state()
@property
def name(self):
"""Return the name of this Switch device if any."""
return self.node_name
@property
def device_state_attributes(self):
"""Show Device Attributes."""
return self.attributes
2018-01-15 22:24:12 +00:00
@property
def current_power_w(self):
"""Return the current power usage in W."""
return self.session.switch.get_power_usage(self.node_id)
@property
def is_on(self):
"""Return true if switch is on."""
return self.session.switch.get_state(self.node_id)
def turn_on(self, **kwargs):
"""Turn the switch on."""
self.session.switch.turn_on(self.node_id)
for entity in self.session.entities:
entity.handle_update(self.data_updatesource)
def turn_off(self, **kwargs):
"""Turn the device off."""
self.session.switch.turn_off(self.node_id)
for entity in self.session.entities:
entity.handle_update(self.data_updatesource)
def update(self):
2018-01-27 19:58:27 +00:00
"""Update all Node data from Hive."""
2018-01-15 22:24:12 +00:00
self.session.core.update_data(self.node_id)
self.attributes = self.session.attributes.state_attributes(
self.node_id)