core/homeassistant/components/hive/switch.py

83 lines
2.4 KiB
Python
Raw Normal View History

"""Support for the Hive switches."""
from datetime import timedelta
from homeassistant.components.switch import SwitchEntity
2018-01-15 22:24:12 +00:00
from . import ATTR_AVAILABLE, ATTR_MODE, DATA_HIVE, DOMAIN, HiveEntity, refresh_system
PARALLEL_UPDATES = 0
SCAN_INTERVAL = timedelta(seconds=15)
2018-01-15 22:24:12 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Hive Switch."""
2018-01-15 22:24:12 +00:00
if discovery_info is None:
return
hive = hass.data[DOMAIN].get(DATA_HIVE)
devices = hive.devices.get("switch")
entities = []
if devices:
for dev in devices:
entities.append(HiveDevicePlug(hive, dev))
async_add_entities(entities, True)
2018-01-15 22:24:12 +00:00
class HiveDevicePlug(HiveEntity, SwitchEntity):
2018-01-15 22:24:12 +00:00
"""Hive Active Plug."""
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."""
2019-07-31 19:25:30 +00:00
return {"identifiers": {(DOMAIN, self.unique_id)}, "name": self.name}
2019-01-09 04:15:12 +00:00
2018-01-15 22:24:12 +00:00
@property
def name(self):
"""Return the name of this Switch device if any."""
return self.device["haName"]
@property
def available(self):
"""Return if the device is available."""
return self.device["deviceData"].get("online")
2018-01-15 22:24:12 +00:00
@property
def device_state_attributes(self):
"""Show Device Attributes."""
return {
ATTR_AVAILABLE: self.attributes.get(ATTR_AVAILABLE),
ATTR_MODE: self.attributes.get(ATTR_MODE),
}
2018-01-15 22:24:12 +00:00
@property
def current_power_w(self):
"""Return the current power usage in W."""
return self.device["status"]["power_usage"]
2018-01-15 22:24:12 +00:00
@property
def is_on(self):
"""Return true if switch is on."""
return self.device["status"]["state"]
2018-01-15 22:24:12 +00:00
@refresh_system
async def async_turn_on(self, **kwargs):
2018-01-15 22:24:12 +00:00
"""Turn the switch on."""
if self.device["hiveType"] == "activeplug":
await self.hive.switch.turn_on(self.device)
2018-01-15 22:24:12 +00:00
@refresh_system
async def async_turn_off(self, **kwargs):
2018-01-15 22:24:12 +00:00
"""Turn the device off."""
if self.device["hiveType"] == "activeplug":
await self.hive.switch.turn_off(self.device)
2018-01-15 22:24:12 +00:00
async def async_update(self):
2018-01-27 19:58:27 +00:00
"""Update all Node data from Hive."""
await self.hive.session.updateData(self.device)
self.device = await self.hive.switch.get_plug(self.device)