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
Refactor Tradfri switch device #26864
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d5fa0e4
Refactor Tradfri switch device
ggravlingen 481069d
Lint
ggravlingen 63656da
Lint
ggravlingen 7fcb98e
Removed unused constant
ggravlingen 9b2f0d3
Add base_class
ggravlingen 7c3ee08
Lint
ggravlingen 6ca1516
Improvements after review
ggravlingen d5251ed
Typo
ggravlingen File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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,96 @@ | ||
"""Base class for IKEA TRADFRI.""" | ||
import logging | ||
|
||
from pytradfri.error import PytradfriError | ||
|
||
from homeassistant.core import callback | ||
from homeassistant.helpers.entity import Entity | ||
from . import DOMAIN as TRADFRI_DOMAIN | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class TradfriBaseDevice(Entity): | ||
"""Base class for a TRADFRI device.""" | ||
|
||
def __init__(self, device, api, gateway_id): | ||
"""Initialize a device.""" | ||
self._available = True | ||
self._api = api | ||
self._device = None | ||
self._device_control = None | ||
self._device_data = None | ||
self._gateway_id = gateway_id | ||
self._name = None | ||
self._unique_id = None | ||
|
||
self._refresh(device) | ||
|
||
@callback | ||
def _async_start_observe(self, exc=None): | ||
"""Start observation of device.""" | ||
if exc: | ||
self._available = False | ||
self.async_schedule_update_ha_state() | ||
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc) | ||
|
||
try: | ||
cmd = self._device.observe( | ||
callback=self._observe_update, | ||
err_callback=self._async_start_observe, | ||
duration=0, | ||
) | ||
self.hass.async_create_task(self._api(cmd)) | ||
except PytradfriError as err: | ||
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. What can raise here ? You are using create task so you're not waiting for the result. |
||
_LOGGER.warning("Observation failed, trying again", exc_info=err) | ||
self._async_start_observe() | ||
|
||
async def async_added_to_hass(self): | ||
"""Start thread when added to hass.""" | ||
self._async_start_observe() | ||
|
||
@property | ||
def available(self): | ||
"""Return True if entity is available.""" | ||
return self._available | ||
|
||
@property | ||
def device_info(self): | ||
"""Return the device info.""" | ||
info = self._device.device_info | ||
|
||
return { | ||
"identifiers": {(TRADFRI_DOMAIN, self._device.id)}, | ||
"name": self._name, | ||
"manufacturer": info.manufacturer, | ||
"model": info.model_number, | ||
"sw_version": info.firmware_version, | ||
"via_device": (TRADFRI_DOMAIN, self._gateway_id), | ||
} | ||
|
||
@property | ||
def name(self): | ||
"""Return the display name of this device.""" | ||
return self._name | ||
|
||
@property | ||
def should_poll(self): | ||
"""No polling needed for tradfri device.""" | ||
return False | ||
|
||
@property | ||
def unique_id(self): | ||
"""Return unique ID for device.""" | ||
return self._unique_id | ||
|
||
@callback | ||
def _observe_update(self, device): | ||
"""Receive new state data for this device.""" | ||
self._refresh(device) | ||
self.async_schedule_update_ha_state() | ||
|
||
def _refresh(self, device): | ||
"""Refresh the device data.""" | ||
self._device = device | ||
self._name = device.name | ||
self._available = device.reachable |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intentional to keep executing the rest of the function ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And if observation did fail, should you retry right away? Should we have some back off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid I'm not very familiar with how this part of the code works (looks like you and Lwis wrote it a long time ago.) I know as much that the
_async_start_observe
looks more or less the same in all of the Tradfri device types, so we should be able to move it into the new base class.However, I wouldn't know what I'm doing when making changes to
_async_start_observe
. It would be a lot of guessing on my end. I'd be happy to just put in whatever you suggests. But that will require time for you to figure out the code. Perhaps a better idea to leave this method as is in the switch-file?