Skip to content
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 8 commits into from Oct 5, 2019
Merged

Refactor Tradfri switch device #26864

merged 8 commits into from Oct 5, 2019

Conversation

ggravlingen
Copy link
Contributor

@ggravlingen ggravlingen commented Sep 23, 2019

Description:

As I was adding support for the Tradfri blinds, I realized there is a lot of repeated code in the different files of the Tradfri lib. This is the first step in refactoring the code.

Related issue (if applicable): relates to #26863

Pull request with documentation for home-assistant.io (if applicable): home-assistant/home-assistant.io#<home-assistant.io PR number goes here>

Example entry for configuration.yaml (if applicable):

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly. Update and include derived files by running python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt by running python3 -m script.gen_requirements_all.
  • Untested files have been added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@project-bot project-bot bot added this to Needs review in Dev Sep 23, 2019
@ggravlingen ggravlingen changed the title Refactor Tradfri switch device WIP Refactor Tradfri switch device Sep 23, 2019

TRADFRI_SWITCH_MANAGER = "Tradfri Switch Manager"
TRADFRI_device_MANAGER = "Tradfri Switch Manager"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is for. Does this have any functionality in Home Assistant's inner workings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it doesn't seem so. We can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I removed it

@MartinHjelmare MartinHjelmare moved this from Needs review to Incoming in Dev Sep 24, 2019
@ggravlingen ggravlingen changed the title WIP Refactor Tradfri switch device Refactor Tradfri switch device Sep 24, 2019
@ggravlingen
Copy link
Contributor Author

Pylint throws errors for no-member on files I haven't altered. How should I best proceed from here?

Dev automation moved this from Incoming to Review in progress Sep 24, 2019
homeassistant/components/tradfri/base_class.py Outdated Show resolved Hide resolved
homeassistant/components/tradfri/base_class.py Outdated Show resolved Hide resolved
homeassistant/components/tradfri/base_class.py Outdated Show resolved Hide resolved
homeassistant/components/tradfri/base_class.py Outdated Show resolved Hide resolved
homeassistant/components/tradfri/base_class.py Outdated Show resolved Hide resolved
@ggravlingen
Copy link
Contributor Author

According to Discord chats, there is some error causing pylint to fail. I’ll wait for the fix and rebase.

self._available = False
self.async_schedule_update_ha_state()
_LOGGER.warning("Observation failed for %s", self._name, exc_info=exc)

Copy link
Member

@balloob balloob Sep 25, 2019

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 ?

Copy link
Member

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.

Copy link
Contributor Author

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?

duration=0,
)
self.hass.async_create_task(self._api(cmd))
except PytradfriError as err:
Copy link
Member

Choose a reason for hiding this comment

The 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.

@ggravlingen
Copy link
Contributor Author

@MartinHjelmare @balloob can this be merged on the basis of my comment above? The intent of this PR, along with a few subsequent ones, is just to remove code that is repeated across the Tradfri-files as well as stale constants.

Once that is done, it will be much less work addressing the issues @balloob raises above.

@MartinHjelmare
Copy link
Member

I think we can merge. @balloob ?

Dev automation moved this from Review in progress to Reviewer approved Oct 5, 2019
@balloob balloob merged commit 5ae497b into home-assistant:dev Oct 5, 2019
Dev automation moved this from Reviewer approved to Done Oct 5, 2019
@ggravlingen ggravlingen mentioned this pull request Oct 6, 2019
9 tasks
@lock lock bot locked and limited conversation to collaborators Oct 6, 2019
@ggravlingen ggravlingen deleted the refactortradfri branch October 13, 2019 09:10
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants