-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Move myStrom light and switch to async #34079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| """Constants for the myStrom integration.""" | ||
| DOMAIN = "mystrom" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,8 @@ | ||
| """Support for myStrom switches.""" | ||
| """Support for myStrom switches/plugs.""" | ||
| import logging | ||
|
|
||
| from pymystrom.exceptions import MyStromConnectionError | ||
| from pymystrom.switch import MyStromPlug | ||
| from pymystrom.switch import MyStromSwitch as _MyStromSwitch | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice | ||
|
|
@@ -22,30 +22,30 @@ | |
| ) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_entities, discovery_info=None): | ||
| """Find and return myStrom switch.""" | ||
| async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): | ||
| """Set up the myStrom switch/plug integration.""" | ||
| name = config.get(CONF_NAME) | ||
| host = config.get(CONF_HOST) | ||
|
|
||
| try: | ||
| MyStromPlug(host).get_status() | ||
| plug = _MyStromSwitch(host) | ||
| await plug.get_state() | ||
| except MyStromConnectionError: | ||
| _LOGGER.error("No route to device: %s", host) | ||
| _LOGGER.error("No route to myStrom plug: %s", host) | ||
| raise PlatformNotReady() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was missing from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it was. |
||
|
|
||
| add_entities([MyStromSwitch(name, host)]) | ||
| async_add_entities([MyStromSwitch(plug, name)]) | ||
|
|
||
|
|
||
| class MyStromSwitch(SwitchDevice): | ||
| """Representation of a myStrom switch.""" | ||
| """Representation of a myStrom switch/plug.""" | ||
|
|
||
| def __init__(self, name, resource): | ||
| """Initialize the myStrom switch.""" | ||
| def __init__(self, plug, name): | ||
| """Initialize the myStrom switch/plug.""" | ||
| self._name = name | ||
| self._resource = resource | ||
| self.data = {} | ||
| self.plug = MyStromPlug(self._resource) | ||
| self.plug = plug | ||
| self._available = True | ||
| self.relay = None | ||
|
|
||
| @property | ||
| def name(self): | ||
|
|
@@ -55,38 +55,43 @@ def name(self): | |
| @property | ||
| def is_on(self): | ||
| """Return true if switch is on.""" | ||
| return bool(self.data["relay"]) | ||
| return bool(self.relay) | ||
|
|
||
| @property | ||
| def unique_id(self): | ||
| """Return a unique ID.""" | ||
| return self.plug._mac # pylint: disable=protected-access | ||
|
|
||
| @property | ||
| def current_power_w(self): | ||
| """Return the current power consumption in W.""" | ||
| return round(self.data["power"], 2) | ||
| return self.plug.consumption | ||
|
|
||
| @property | ||
| def available(self): | ||
| """Could the device be accessed during the last update call.""" | ||
| return self._available | ||
|
|
||
| def turn_on(self, **kwargs): | ||
| async def async_turn_on(self, **kwargs): | ||
| """Turn the switch on.""" | ||
| try: | ||
| self.plug.set_relay_on() | ||
| await self.plug.turn_on() | ||
| except MyStromConnectionError: | ||
| _LOGGER.error("No route to device: %s", self._resource) | ||
| _LOGGER.error("No route to myStrom plug") | ||
|
|
||
| def turn_off(self, **kwargs): | ||
| async def async_turn_off(self, **kwargs): | ||
| """Turn the switch off.""" | ||
| try: | ||
| self.plug.set_relay_off() | ||
| await self.plug.turn_off() | ||
| except MyStromConnectionError: | ||
| _LOGGER.error("No route to device: %s", self._resource) | ||
| _LOGGER.error("No route to myStrom plug") | ||
|
|
||
| def update(self): | ||
| async def async_update(self): | ||
| """Get the latest data from the device and update the data.""" | ||
| try: | ||
| self.data = self.plug.get_status() | ||
| await self.plug.get_state() | ||
| self.relay = self.plug.relay | ||
| self._available = True | ||
| except MyStromConnectionError: | ||
| self.data = {"power": 0, "relay": False} | ||
| self._available = False | ||
| _LOGGER.error("No route to device: %s", self._resource) | ||
| _LOGGER.error("No route to myStrom plug") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.