core/homeassistant/components/hive/switch.py

87 lines
2.6 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 HiveEntity, refresh_system
from .const import ATTR_MODE, DOMAIN
PARALLEL_UPDATES = 0
SCAN_INTERVAL = timedelta(seconds=15)
2018-01-15 22:24:12 +00:00
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Hive thermostat based on a config entry."""
2018-01-15 22:24:12 +00:00
hive = hass.data[DOMAIN][entry.entry_id]
devices = hive.session.deviceList.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."""
if self.device["hiveType"] == "activeplug":
return {
"identifiers": {(DOMAIN, self.device["device_id"])},
"name": self.device["device_name"],
"model": self.device["deviceData"]["model"],
"manufacturer": self.device["deviceData"]["manufacturer"],
"sw_version": self.device["deviceData"]["version"],
"via_device": (DOMAIN, self.device["parentDevice"]),
}
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 extra_state_attributes(self):
"""Show Device Attributes."""
return {
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."""
2021-04-01 23:14:40 +00:00
return self.device["status"].get("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."""
await self.hive.switch.turnOn(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."""
await self.hive.switch.turnOff(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)
2021-04-01 23:14:40 +00:00
self.device = await self.hive.switch.getSwitch(self.device)