2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Xiaomi Aqara binary sensors."""
|
2017-07-20 13:20:00 +00:00
|
|
|
import logging
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-06-22 09:54:17 +00:00
|
|
|
from . import XiaomiDevice
|
|
|
|
from .const import DOMAIN, GATEWAYS_KEY
|
2017-07-20 13:20:00 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-08-13 07:54:43 +00:00
|
|
|
# Load power in watts (W)
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LOAD_POWER = "load_power"
|
2017-08-13 07:54:43 +00:00
|
|
|
|
|
|
|
# Total (lifetime) power consumption in watts
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_POWER_CONSUMED = "power_consumed"
|
|
|
|
ATTR_IN_USE = "in_use"
|
2017-08-13 07:54:43 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
LOAD_POWER = "load_power"
|
|
|
|
POWER_CONSUMED = "power_consumed"
|
|
|
|
ENERGY_CONSUMED = "energy_consumed"
|
|
|
|
IN_USE = "inuse"
|
2017-07-20 13:20:00 +00:00
|
|
|
|
|
|
|
|
2020-06-22 09:54:17 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
2017-07-20 13:20:00 +00:00
|
|
|
"""Perform the setup for Xiaomi devices."""
|
2020-06-22 09:54:17 +00:00
|
|
|
entities = []
|
|
|
|
gateway = hass.data[DOMAIN][GATEWAYS_KEY][config_entry.entry_id]
|
|
|
|
for device in gateway.devices["switch"]:
|
|
|
|
model = device["model"]
|
|
|
|
if model == "plug":
|
|
|
|
if "proto" not in device or int(device["proto"][0:1]) == 1:
|
|
|
|
data_key = "status"
|
|
|
|
else:
|
|
|
|
data_key = "channel_0"
|
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device, "Plug", data_key, True, gateway, config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
2020-08-05 10:15:19 +00:00
|
|
|
elif model in ["ctrl_neutral1", "ctrl_neutral1.aq1", "switch_b1lacn02"]:
|
2020-06-22 09:54:17 +00:00
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device, "Wall Switch", "channel_0", False, gateway, config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
2020-08-05 10:15:19 +00:00
|
|
|
elif model in ["ctrl_ln1", "ctrl_ln1.aq1", "switch_b1nacn02"]:
|
2020-06-22 09:54:17 +00:00
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device, "Wall Switch LN", "channel_0", False, gateway, config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
2020-08-05 10:15:19 +00:00
|
|
|
elif model in ["ctrl_neutral2", "ctrl_neutral2.aq1", "switch_b2lacn02"]:
|
2020-06-22 09:54:17 +00:00
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device,
|
|
|
|
"Wall Switch Left",
|
|
|
|
"channel_0",
|
|
|
|
False,
|
|
|
|
gateway,
|
|
|
|
config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device,
|
|
|
|
"Wall Switch Right",
|
|
|
|
"channel_1",
|
|
|
|
False,
|
|
|
|
gateway,
|
|
|
|
config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
2020-08-05 10:15:19 +00:00
|
|
|
elif model in ["ctrl_ln2", "ctrl_ln2.aq1", "switch_b2nacn02"]:
|
2020-06-22 09:54:17 +00:00
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device,
|
|
|
|
"Wall Switch LN Left",
|
|
|
|
"channel_0",
|
|
|
|
False,
|
|
|
|
gateway,
|
|
|
|
config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device,
|
|
|
|
"Wall Switch LN Right",
|
|
|
|
"channel_1",
|
|
|
|
False,
|
|
|
|
gateway,
|
|
|
|
config_entry,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
|
|
|
elif model in ["86plug", "ctrl_86plug", "ctrl_86plug.aq1"]:
|
|
|
|
if "proto" not in device or int(device["proto"][0:1]) == 1:
|
|
|
|
data_key = "status"
|
|
|
|
else:
|
|
|
|
data_key = "channel_0"
|
|
|
|
entities.append(
|
|
|
|
XiaomiGenericSwitch(
|
|
|
|
device, "Wall Plug", data_key, True, gateway, config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-06-22 09:54:17 +00:00
|
|
|
)
|
|
|
|
async_add_entities(entities)
|
2017-07-20 13:20:00 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class XiaomiGenericSwitch(XiaomiDevice, SwitchEntity):
|
2017-07-20 13:20:00 +00:00
|
|
|
"""Representation of a XiaomiPlug."""
|
|
|
|
|
2020-06-22 09:54:17 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
device,
|
|
|
|
name,
|
|
|
|
data_key,
|
|
|
|
supports_power_consumption,
|
|
|
|
xiaomi_hub,
|
|
|
|
config_entry,
|
|
|
|
):
|
2017-07-20 13:20:00 +00:00
|
|
|
"""Initialize the XiaomiPlug."""
|
|
|
|
self._data_key = data_key
|
|
|
|
self._in_use = None
|
|
|
|
self._load_power = None
|
|
|
|
self._power_consumed = None
|
|
|
|
self._supports_power_consumption = supports_power_consumption
|
2020-06-22 09:54:17 +00:00
|
|
|
super().__init__(device, name, xiaomi_hub, config_entry)
|
2017-07-20 13:20:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._data_key == "status":
|
|
|
|
return "mdi:power-plug"
|
|
|
|
return "mdi:power-socket"
|
2017-07-20 13:20:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if it is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
if self._supports_power_consumption:
|
2019-02-13 20:21:14 +00:00
|
|
|
attrs = {
|
|
|
|
ATTR_IN_USE: self._in_use,
|
|
|
|
ATTR_LOAD_POWER: self._load_power,
|
|
|
|
ATTR_POWER_CONSUMED: self._power_consumed,
|
|
|
|
}
|
2017-07-20 13:20:00 +00:00
|
|
|
else:
|
|
|
|
attrs = {}
|
|
|
|
attrs.update(super().device_state_attributes)
|
|
|
|
return attrs
|
|
|
|
|
2018-10-01 16:00:48 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2018-10-24 16:59:52 +00:00
|
|
|
"""Return the polling state. Polling needed for Zigbee plug only."""
|
2018-10-01 16:00:48 +00:00
|
|
|
return self._supports_power_consumption
|
|
|
|
|
2017-07-20 13:20:00 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._write_to_hub(self._sid, **{self._data_key: "on"}):
|
2017-07-20 13:20:00 +00:00
|
|
|
self._state = True
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
2018-02-11 17:20:28 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2017-07-20 13:20:00 +00:00
|
|
|
"""Turn the switch off."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._write_to_hub(self._sid, **{self._data_key: "off"}):
|
2017-07-20 13:20:00 +00:00
|
|
|
self._state = False
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
2018-01-23 09:22:43 +00:00
|
|
|
def parse_data(self, data, raw_data):
|
2017-07-20 13:20:00 +00:00
|
|
|
"""Parse data sent by gateway."""
|
|
|
|
if IN_USE in data:
|
|
|
|
self._in_use = int(data[IN_USE])
|
|
|
|
if not self._in_use:
|
|
|
|
self._load_power = 0
|
2018-10-08 08:37:27 +00:00
|
|
|
|
|
|
|
for key in [POWER_CONSUMED, ENERGY_CONSUMED]:
|
|
|
|
if key in data:
|
|
|
|
self._power_consumed = round(float(data[key]), 2)
|
|
|
|
break
|
|
|
|
|
2017-07-20 13:20:00 +00:00
|
|
|
if LOAD_POWER in data:
|
|
|
|
self._load_power = round(float(data[LOAD_POWER]), 2)
|
|
|
|
|
|
|
|
value = data.get(self._data_key)
|
2019-07-31 19:25:30 +00:00
|
|
|
if value not in ["on", "off"]:
|
2017-07-20 13:20:00 +00:00
|
|
|
return False
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state = value == "on"
|
2017-07-20 13:20:00 +00:00
|
|
|
if self._state == state:
|
|
|
|
return False
|
|
|
|
self._state = state
|
|
|
|
return True
|
2018-10-01 16:00:48 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get data from hub."""
|
|
|
|
_LOGGER.debug("Update data from hub: %s", self._name)
|
|
|
|
self._get_from_hub(self._sid)
|