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 <paulus@home-assistant.io>
This commit is contained in:
Josh Anderson 2020-02-09 00:45:28 +00:00 committed by GitHub
parent a2bea2cab8
commit 9987978d1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 11 deletions

View file

@ -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": []
}

View file

@ -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"

View file

@ -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