2019-02-13 20:21:14 +00:00
|
|
|
"""Support for WeMo switches."""
|
2018-03-12 20:54:56 +00:00
|
|
|
import asyncio
|
2017-02-15 20:34:42 +00:00
|
|
|
from datetime import datetime, timedelta
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
from pywemo import CoffeeMaker, Insight, Maker
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2019-12-09 10:53:51 +00:00
|
|
|
from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN
|
2020-01-19 20:56:31 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
2017-03-19 21:02:11 +00:00
|
|
|
from homeassistant.util import convert
|
2014-11-12 05:39:17 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
from .const import DOMAIN as WEMO_DOMAIN
|
2021-08-21 18:14:55 +00:00
|
|
|
from .entity import WemoEntity
|
2019-04-12 06:37:45 +00:00
|
|
|
|
2018-03-12 20:54:56 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
2020-01-19 20:56:31 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
2015-08-09 04:22:34 +00:00
|
|
|
|
2020-06-16 19:14:04 +00:00
|
|
|
# The WEMO_ constants below come from pywemo itself
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_SENSOR_STATE = "sensor_state"
|
|
|
|
ATTR_SWITCH_MODE = "switch_mode"
|
|
|
|
ATTR_CURRENT_STATE_DETAIL = "state_detail"
|
|
|
|
ATTR_COFFEMAKER_MODE = "coffeemaker_mode"
|
2015-12-23 15:57:51 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
MAKER_SWITCH_MOMENTARY = "momentary"
|
|
|
|
MAKER_SWITCH_TOGGLE = "toggle"
|
2016-02-25 22:46:14 +00:00
|
|
|
|
|
|
|
WEMO_ON = 1
|
|
|
|
WEMO_OFF = 0
|
|
|
|
WEMO_STANDBY = 8
|
|
|
|
|
2016-01-20 11:25:20 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up WeMo switches."""
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
async def _discovered_wemo(coordinator):
|
2020-01-19 20:56:31 +00:00
|
|
|
"""Handle a discovered Wemo device."""
|
2021-08-21 18:14:55 +00:00
|
|
|
async_add_entities([WemoSwitch(coordinator)])
|
2018-08-16 14:14:54 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
async_dispatcher_connect(hass, f"{WEMO_DOMAIN}.switch", _discovered_wemo)
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2020-01-19 20:56:31 +00:00
|
|
|
await asyncio.gather(
|
2021-07-19 08:46:09 +00:00
|
|
|
*(
|
2021-08-21 18:14:55 +00:00
|
|
|
_discovered_wemo(coordinator)
|
|
|
|
for coordinator in hass.data[WEMO_DOMAIN]["pending"].pop("switch")
|
2021-07-19 08:46:09 +00:00
|
|
|
)
|
2020-01-19 20:56:31 +00:00
|
|
|
)
|
2015-03-01 09:35:58 +00:00
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
class WemoSwitch(WemoEntity, SwitchEntity):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Representation of a WeMo switch."""
|
|
|
|
|
2016-01-20 11:06:08 +00:00
|
|
|
@property
|
2021-03-11 19:16:26 +00:00
|
|
|
def extra_state_attributes(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2016-02-07 06:28:29 +00:00
|
|
|
attr = {}
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, Maker):
|
2016-01-20 11:06:08 +00:00
|
|
|
# Is the maker sensor on or off.
|
2021-08-21 18:14:55 +00:00
|
|
|
if self.wemo.maker_params["hassensor"]:
|
2016-01-20 11:06:08 +00:00
|
|
|
# Note a state of 1 matches the WeMo app 'not triggered'!
|
2021-08-21 18:14:55 +00:00
|
|
|
if self.wemo.maker_params["sensorstate"]:
|
2016-01-20 11:06:08 +00:00
|
|
|
attr[ATTR_SENSOR_STATE] = STATE_OFF
|
|
|
|
else:
|
|
|
|
attr[ATTR_SENSOR_STATE] = STATE_ON
|
|
|
|
|
|
|
|
# Is the maker switch configured as toggle(0) or momentary (1).
|
2021-08-21 18:14:55 +00:00
|
|
|
if self.wemo.maker_params["switchmode"]:
|
2016-01-20 19:13:29 +00:00
|
|
|
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_MOMENTARY
|
|
|
|
else:
|
|
|
|
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE
|
2016-01-20 11:06:08 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, (Insight, CoffeeMaker)):
|
2016-02-25 22:46:14 +00:00
|
|
|
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
|
2017-02-23 23:03:49 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, Insight):
|
|
|
|
attr["on_latest_time"] = WemoSwitch.as_uptime(
|
2021-08-26 08:27:06 +00:00
|
|
|
self.wemo.insight_params.get("onfor", 0)
|
2021-08-21 18:14:55 +00:00
|
|
|
)
|
|
|
|
attr["on_today_time"] = WemoSwitch.as_uptime(
|
2021-08-26 08:27:06 +00:00
|
|
|
self.wemo.insight_params.get("ontoday", 0)
|
2021-08-21 18:14:55 +00:00
|
|
|
)
|
|
|
|
attr["on_total_time"] = WemoSwitch.as_uptime(
|
2021-08-26 08:27:06 +00:00
|
|
|
self.wemo.insight_params.get("ontotal", 0)
|
2021-08-21 18:14:55 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
attr["power_threshold_w"] = (
|
2021-08-26 08:27:06 +00:00
|
|
|
convert(self.wemo.insight_params.get("powerthreshold"), float, 0.0)
|
|
|
|
/ 1000.0
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-01-20 11:06:08 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, CoffeeMaker):
|
|
|
|
attr[ATTR_COFFEMAKER_MODE] = self.wemo.mode
|
2017-01-25 06:10:10 +00:00
|
|
|
|
2016-02-25 22:46:14 +00:00
|
|
|
return attr
|
2015-08-31 10:07:52 +00:00
|
|
|
|
2017-02-15 21:47:02 +00:00
|
|
|
@staticmethod
|
|
|
|
def as_uptime(_seconds):
|
2017-02-15 22:32:45 +00:00
|
|
|
"""Format seconds into uptime string in the format: 00d 00h 00m 00s."""
|
2017-02-15 21:47:02 +00:00
|
|
|
uptime = datetime(1, 1, 1) + timedelta(seconds=_seconds)
|
2017-05-02 20:47:20 +00:00
|
|
|
return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(
|
2019-07-31 19:25:30 +00:00
|
|
|
uptime.day - 1, uptime.hour, uptime.minute, uptime.second
|
|
|
|
)
|
2017-02-15 21:47:02 +00:00
|
|
|
|
2017-02-14 23:29:23 +00:00
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the current power usage in W."""
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, Insight):
|
|
|
|
return (
|
2021-08-26 08:27:06 +00:00
|
|
|
convert(self.wemo.insight_params.get("currentpower"), float, 0.0)
|
|
|
|
/ 1000.0
|
2021-08-21 18:14:55 +00:00
|
|
|
)
|
2014-12-16 03:14:31 +00:00
|
|
|
|
2015-06-13 21:56:20 +00:00
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def today_energy_kwh(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the today total energy usage in kWh."""
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, Insight):
|
2021-08-26 08:27:06 +00:00
|
|
|
miliwatts = convert(self.wemo.insight_params.get("todaymw"), float, 0.0)
|
2017-03-29 19:49:28 +00:00
|
|
|
return round(miliwatts / (1000.0 * 1000.0 * 60), 2)
|
2015-01-11 17:20:41 +00:00
|
|
|
|
2015-08-28 22:11:55 +00:00
|
|
|
@property
|
2016-02-25 22:46:14 +00:00
|
|
|
def detail_state(self):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return the state of the device."""
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, CoffeeMaker):
|
|
|
|
return self.wemo.mode_string
|
|
|
|
if isinstance(self.wemo, Insight):
|
2021-08-26 08:27:06 +00:00
|
|
|
standby_state = int(self.wemo.insight_params.get("state", 0))
|
2016-02-25 22:46:14 +00:00
|
|
|
if standby_state == WEMO_ON:
|
2016-02-26 08:28:17 +00:00
|
|
|
return STATE_ON
|
2018-07-23 08:16:05 +00:00
|
|
|
if standby_state == WEMO_OFF:
|
2016-02-25 22:46:14 +00:00
|
|
|
return STATE_OFF
|
2018-07-23 08:16:05 +00:00
|
|
|
if standby_state == WEMO_STANDBY:
|
2016-02-25 22:46:14 +00:00
|
|
|
return STATE_STANDBY
|
2017-07-06 06:30:01 +00:00
|
|
|
return STATE_UNKNOWN
|
2015-08-28 22:11:55 +00:00
|
|
|
|
2017-01-25 06:10:10 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Return the icon of device based on its type."""
|
2021-08-21 18:14:55 +00:00
|
|
|
if isinstance(self.wemo, CoffeeMaker):
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:coffee"
|
2017-07-06 06:30:01 +00:00
|
|
|
return None
|
2017-01-25 06:10:10 +00:00
|
|
|
|
2021-08-21 18:14:55 +00:00
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if the state is on. Standby is on."""
|
|
|
|
return self.wemo.get_state()
|
|
|
|
|
2015-01-11 17:20:41 +00:00
|
|
|
def turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2021-02-17 22:36:39 +00:00
|
|
|
with self._wemo_exception_handler("turn on"):
|
2021-08-21 18:14:55 +00:00
|
|
|
self.wemo.on()
|
2015-01-11 17:20:41 +00:00
|
|
|
|
2020-06-16 19:14:04 +00:00
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
2017-07-06 03:02:16 +00:00
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2021-02-17 22:36:39 +00:00
|
|
|
with self._wemo_exception_handler("turn off"):
|
2021-08-21 18:14:55 +00:00
|
|
|
self.wemo.off()
|
2015-01-16 05:25:24 +00:00
|
|
|
|
2020-06-16 19:14:04 +00:00
|
|
|
self.schedule_update_ha_state()
|