From 9987978d1a71f8c2e1828bceb57188fc0e1bd7ef Mon Sep 17 00:00:00 2001 From: Josh Anderson Date: Sun, 9 Feb 2020 00:45:28 +0000 Subject: [PATCH] Add unique ID to edimax switches (#27984) * Add unique ID and device info data * Don't get power info on switch models lacking it * Move info fetching to update, update before adding * Upgrade pyedimax to get device info * Remove device info Co-authored-by: Paulus Schoutsen --- homeassistant/components/edimax/manifest.json | 2 +- homeassistant/components/edimax/switch.py | 34 ++++++++++++++----- requirements_all.txt | 2 +- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/edimax/manifest.json b/homeassistant/components/edimax/manifest.json index 20036311592..de8b978b9f9 100644 --- a/homeassistant/components/edimax/manifest.json +++ b/homeassistant/components/edimax/manifest.json @@ -2,7 +2,7 @@ "domain": "edimax", "name": "Edimax", "documentation": "https://www.home-assistant.io/integrations/edimax", - "requirements": ["pyedimax==0.1"], + "requirements": ["pyedimax==0.2.1"], "dependencies": [], "codeowners": [] } diff --git a/homeassistant/components/edimax/switch.py b/homeassistant/components/edimax/switch.py index 3d558f6c770..e44ec23bca7 100644 --- a/homeassistant/components/edimax/switch.py +++ b/homeassistant/components/edimax/switch.py @@ -10,6 +10,8 @@ import homeassistant.helpers.config_validation as cv _LOGGER = logging.getLogger(__name__) +DOMAIN = "edimax" + DEFAULT_NAME = "Edimax Smart Plug" DEFAULT_PASSWORD = "1234" DEFAULT_USERNAME = "admin" @@ -30,7 +32,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): auth = (config.get(CONF_USERNAME), config.get(CONF_PASSWORD)) name = config.get(CONF_NAME) - add_entities([SmartPlugSwitch(SmartPlug(host, auth), name)]) + add_entities([SmartPlugSwitch(SmartPlug(host, auth), name)], True) class SmartPlugSwitch(SwitchDevice): @@ -43,6 +45,14 @@ class SmartPlugSwitch(SwitchDevice): self._now_power = None self._now_energy_day = None self._state = False + self._supports_power_monitoring = False + self._info = None + self._mac = None + + @property + def unique_id(self): + """Return the device's MAC address.""" + return self._mac @property def name(self): @@ -74,14 +84,20 @@ class SmartPlugSwitch(SwitchDevice): def update(self): """Update edimax switch.""" - try: - self._now_power = float(self.smartplug.now_power) - except (TypeError, ValueError): - self._now_power = None + if not self._info: + self._info = self.smartplug.info + self._mac = self._info["mac"] + self._supports_power_monitoring = self._info["model"] != "SP1101W" - try: - self._now_energy_day = float(self.smartplug.now_energy_day) - except (TypeError, ValueError): - self._now_energy_day = None + if self._supports_power_monitoring: + try: + self._now_power = float(self.smartplug.now_power) + except (TypeError, ValueError): + self._now_power = None + + try: + self._now_energy_day = float(self.smartplug.now_energy_day) + except (TypeError, ValueError): + self._now_energy_day = None self._state = self.smartplug.state == "ON" diff --git a/requirements_all.txt b/requirements_all.txt index 036cce48405..159e041b731 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1216,7 +1216,7 @@ pyebox==1.1.4 pyeconet==0.0.11 # homeassistant.components.edimax -pyedimax==0.1 +pyedimax==0.2.1 # homeassistant.components.eight_sleep pyeight==0.1.2