Skip to content

Add binary sensor platform to devolo Home Network#60301

Merged
bdraco merged 12 commits intohome-assistant:devfrom
2Fake:hn_binary_sensor
May 9, 2022
Merged

Add binary sensor platform to devolo Home Network#60301
bdraco merged 12 commits intohome-assistant:devfrom
2Fake:hn_binary_sensor

Conversation

@Shutgun
Copy link
Copy Markdown
Contributor

@Shutgun Shutgun commented Nov 24, 2021

Proposed change

Add binary sensor platform to devolo Home Network, starting with two entities: Firmware update and if the device is connected directly to the router. As the state of the later cannot be retrieved only from the coordinator, I implemented if differently than the other binary sensor and the existing sensors.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link
Copy Markdown

Hey there @2Fake, mind taking a look at this pull request as it has been labeled with an integration (devolo_home_network) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Copy link
Copy Markdown
Contributor

@epenet epenet left a comment

Choose a reason for hiding this comment

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

I think that you can improve the use of EntityDescription to have only a single DevoloBinarySensorEntity class.

To do this, you can create two module function _is_on_basic and _is_on_extended which take the entity as parameter, and you set value_func to the new module function accordingly.

@Shutgun
Copy link
Copy Markdown
Contributor Author

Shutgun commented Nov 25, 2021

Hi @epenet , thanks for the hint. It just didn't come to my mind to use the complete entity as parameter ...

Comment on lines 24 to 33

self._device = device
self._device_name = device_name
self.device = device
self.device_name = device_name

self._attr_device_info = DeviceInfo(
configuration_url=f"http://{self._device.ip}",
identifiers={(DOMAIN, str(self._device.serial_number))},
configuration_url=f"http://{self.device.ip}",
identifiers={(DOMAIN, str(self.device.serial_number))},
manufacturer="devolo",
model=self._device.product,
name=self._device_name,
sw_version=self._device.firmware_version,
model=self.device.product,
name=self.device_name,
sw_version=self.device.firmware_version,
)
self._attr_unique_id = (
f"{self._device.serial_number}_{self.entity_description.key}"
f"{self.device.serial_number}_{self.entity_description.key}"
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why drop the _?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I access device in _is_connected_to_router and with _ pylint complains about the private access.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since this is all in __init__, you could simply use device and device_name function arguments instead of self.xxx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm, I'm not seeing it. Could you help me out? The signatures of _is_connected_to_router and the lambda (and any possible upcoming sensor) would need to be the same although _is_connected_to_router is most likely the only one needing it and having the information available inside the entity, right? Isn't then accessing entity.device (and by that making it public) cleaner?

Copy link
Copy Markdown
Contributor

@epenet epenet Nov 26, 2021

Choose a reason for hiding this comment

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

You keep the change at the top (lines 24/25):

        self.device = device
        self.device_name = device_name

But in the subsequent lines instead of replacing self._dev... by self.dev... you replace it by dev... (drop the self._)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it! I was at a complete different spot in the code ...

@bdraco bdraco self-requested a review December 7, 2021 08:41
@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Jan 4, 2022

# Emulate device failure
with patch(
"devolo_plc_api.plcnet_api.plcnetapi.PlcNetApi.async_get_network_overview",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"devolo_plc_api.plcnet_api.plcnetapi.PlcNetApi.async_get_network_overview",
"homeassistant.components.devolo_home_network.devolo_plc_api.plcnet_api.plcnetapi.PlcNetApi.async_get_network_overview",

Usually we patch where the library is imported so we don't accidentally patch places we don't want to change

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this case it would probably be better to adjust the mock_device instead

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The mock_device is just a collection of patches itself. I'm not seeing, how I could adjust it.


# Emulate device failure
with patch(
"devolo_plc_api.device_api.deviceapi.DeviceApi.async_check_firmware_available",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you adjust mock_device instead of patching here?

# Emulate state change
with patch(
"devolo_plc_api.device_api.deviceapi.DeviceApi.async_check_firmware_available",
new=AsyncMock(return_value={"result": "UPDATE_AVAILABLE"}),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you adjust mock_device instead of patching here?

@frenck frenck removed the smash Indicator this PR is close to finish for merging or closing label Jan 5, 2022
@Shutgun Shutgun mentioned this pull request Jan 10, 2022
22 tasks
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 5, 2022

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Apr 5, 2022
@bdraco bdraco self-requested a review April 5, 2022 11:11
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Apr 5, 2022

not stale, awaits response (Didn't see the question until now)

@bdraco bdraco removed the stale label Apr 5, 2022
@Shutgun
Copy link
Copy Markdown
Contributor Author

Shutgun commented Apr 5, 2022

I guess, I should remove the firmware_update_available binary sensor, as update is an own platform by now, right?

@Shutgun
Copy link
Copy Markdown
Contributor Author

Shutgun commented Apr 25, 2022

Hi @bdraco , I removed the update sensor, so I guess I'm ready for review.

@bdraco bdraco merged commit 2b30bda into home-assistant:dev May 9, 2022
@Shutgun Shutgun deleted the hn_binary_sensor branch May 9, 2022 08:34
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants