This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+38 -48
View File
@@ -9,8 +9,7 @@ import async_timeout
from homeassistant.components.switch import SwitchDevice
from homeassistant.exceptions import PlatformNotReady
from homeassistant.util import convert
from homeassistant.const import (
STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN)
from homeassistant.const import STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN
from . import SUBSCRIPTION_REGISTRY, DOMAIN as WEMO_DOMAIN
@@ -18,13 +17,13 @@ SCAN_INTERVAL = timedelta(seconds=10)
_LOGGER = logging.getLogger(__name__)
ATTR_SENSOR_STATE = 'sensor_state'
ATTR_SWITCH_MODE = 'switch_mode'
ATTR_CURRENT_STATE_DETAIL = 'state_detail'
ATTR_COFFEMAKER_MODE = 'coffeemaker_mode'
ATTR_SENSOR_STATE = "sensor_state"
ATTR_SWITCH_MODE = "switch_mode"
ATTR_CURRENT_STATE_DETAIL = "state_detail"
ATTR_COFFEMAKER_MODE = "coffeemaker_mode"
MAKER_SWITCH_MOMENTARY = 'momentary'
MAKER_SWITCH_TOGGLE = 'toggle'
MAKER_SWITCH_MOMENTARY = "momentary"
MAKER_SWITCH_TOGGLE = "toggle"
WEMO_ON = 1
WEMO_OFF = 0
@@ -36,13 +35,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
from pywemo import discovery
if discovery_info is not None:
location = discovery_info['ssdp_description']
mac = discovery_info['mac_address']
location = discovery_info["ssdp_description"]
mac = discovery_info["mac_address"]
try:
device = discovery.device_from_description(location, mac)
except (requests.exceptions.ConnectionError,
requests.exceptions.Timeout) as err:
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
) as err:
_LOGGER.error("Unable to access %s (%s)", location, err)
raise PlatformNotReady
@@ -71,8 +72,7 @@ class WemoSwitch(SwitchDevice):
"""Update the state by the Wemo device."""
_LOGGER.info("Subscription update for %s", self.name)
updated = self.wemo.subscription_update(_type, _params)
self.hass.add_job(
self._async_locked_subscription_callback(not updated))
self.hass.add_job(self._async_locked_subscription_callback(not updated))
async def _async_locked_subscription_callback(self, force_update):
"""Handle an update from a subscription."""
@@ -96,10 +96,7 @@ class WemoSwitch(SwitchDevice):
@property
def device_info(self):
"""Return the device info."""
return {
'name': self._name,
'identifiers': {(WEMO_DOMAIN, self._serialnumber)},
}
return {"name": self._name, "identifiers": {(WEMO_DOMAIN, self._serialnumber)}}
@property
def device_state_attributes(self):
@@ -107,15 +104,15 @@ class WemoSwitch(SwitchDevice):
attr = {}
if self.maker_params:
# Is the maker sensor on or off.
if self.maker_params['hassensor']:
if self.maker_params["hassensor"]:
# Note a state of 1 matches the WeMo app 'not triggered'!
if self.maker_params['sensorstate']:
if self.maker_params["sensorstate"]:
attr[ATTR_SENSOR_STATE] = STATE_OFF
else:
attr[ATTR_SENSOR_STATE] = STATE_ON
# Is the maker switch configured as toggle(0) or momentary (1).
if self.maker_params['switchmode']:
if self.maker_params["switchmode"]:
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_MOMENTARY
else:
attr[ATTR_SWITCH_MODE] = MAKER_SWITCH_TOGGLE
@@ -124,16 +121,12 @@ class WemoSwitch(SwitchDevice):
attr[ATTR_CURRENT_STATE_DETAIL] = self.detail_state
if self.insight_params:
attr['on_latest_time'] = \
WemoSwitch.as_uptime(self.insight_params['onfor'])
attr['on_today_time'] = \
WemoSwitch.as_uptime(self.insight_params['ontoday'])
attr['on_total_time'] = \
WemoSwitch.as_uptime(self.insight_params['ontotal'])
attr['power_threshold_w'] = \
convert(
self.insight_params['powerthreshold'], float, 0.0
) / 1000.0
attr["on_latest_time"] = WemoSwitch.as_uptime(self.insight_params["onfor"])
attr["on_today_time"] = WemoSwitch.as_uptime(self.insight_params["ontoday"])
attr["on_total_time"] = WemoSwitch.as_uptime(self.insight_params["ontotal"])
attr["power_threshold_w"] = (
convert(self.insight_params["powerthreshold"], float, 0.0) / 1000.0
)
if self.coffeemaker_mode is not None:
attr[ATTR_COFFEMAKER_MODE] = self.coffeemaker_mode
@@ -145,21 +138,20 @@ class WemoSwitch(SwitchDevice):
"""Format seconds into uptime string in the format: 00d 00h 00m 00s."""
uptime = datetime(1, 1, 1) + timedelta(seconds=_seconds)
return "{:0>2d}d {:0>2d}h {:0>2d}m {:0>2d}s".format(
uptime.day-1, uptime.hour, uptime.minute, uptime.second)
uptime.day - 1, uptime.hour, uptime.minute, uptime.second
)
@property
def current_power_w(self):
"""Return the current power usage in W."""
if self.insight_params:
return convert(
self.insight_params['currentpower'], float, 0.0
) / 1000.0
return convert(self.insight_params["currentpower"], float, 0.0) / 1000.0
@property
def today_energy_kwh(self):
"""Return the today total energy usage in kWh."""
if self.insight_params:
miliwatts = convert(self.insight_params['todaymw'], float, 0.0)
miliwatts = convert(self.insight_params["todaymw"], float, 0.0)
return round(miliwatts / (1000.0 * 1000.0 * 60), 2)
@property
@@ -168,7 +160,7 @@ class WemoSwitch(SwitchDevice):
if self.coffeemaker_mode is not None:
return self._mode_string
if self.insight_params:
standby_state = int(self.insight_params['state'])
standby_state = int(self.insight_params["state"])
if standby_state == WEMO_ON:
return STATE_ON
if standby_state == WEMO_OFF:
@@ -190,8 +182,8 @@ class WemoSwitch(SwitchDevice):
@property
def icon(self):
"""Return the icon of device based on its type."""
if self._model_name == 'CoffeeMaker':
return 'mdi:coffee'
if self._model_name == "CoffeeMaker":
return "mdi:coffee"
return None
def turn_on(self, **kwargs):
@@ -227,7 +219,7 @@ class WemoSwitch(SwitchDevice):
with async_timeout.timeout(5):
await asyncio.shield(self._async_locked_update(True))
except asyncio.TimeoutError:
_LOGGER.warning('Lost connection to %s', self.name)
_LOGGER.warning("Lost connection to %s", self.name)
self._available = False
async def _async_locked_update(self, force_update):
@@ -240,20 +232,18 @@ class WemoSwitch(SwitchDevice):
try:
self._state = self.wemo.get_state(force_update)
if self._model_name == 'Insight':
if self._model_name == "Insight":
self.insight_params = self.wemo.insight_params
self.insight_params['standby_state'] = (
self.wemo.get_standby_state)
elif self._model_name == 'Maker':
self.insight_params["standby_state"] = self.wemo.get_standby_state
elif self._model_name == "Maker":
self.maker_params = self.wemo.maker_params
elif self._model_name == 'CoffeeMaker':
elif self._model_name == "CoffeeMaker":
self.coffeemaker_mode = self.wemo.mode
self._mode_string = self.wemo.mode_string
if not self._available:
_LOGGER.info('Reconnected to %s', self.name)
_LOGGER.info("Reconnected to %s", self.name)
self._available = True
except AttributeError as err:
_LOGGER.warning("Could not update status for %s (%s)",
self.name, err)
_LOGGER.warning("Could not update status for %s (%s)", self.name, err)
self._available = False