core icon indicating copy to clipboard operation
core copied to clipboard

Add PLC phyrates as sensor to devolo Home Network

Open Shutgun opened this issue 2 years ago • 1 comments

Proposed change

Add PLC phyrates as sensor to devolo Home Network. As this basically adds two sensors per possible data path, I add the main route to the router as enabled and all other as disabled.

Since this sensor is different from the existing ones I added a new base entity and didn't try to force it into the existing entity.

Type of change

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

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: https://github.com/home-assistant/home-assistant.io/pull/26033

Checklist

  • [x] The code change is tested and works locally.
  • [x] Local tests pass. Your PR cannot be merged unless tests pass
  • [x] There is no commented out code in this PR.
  • [x] I have followed the development checklist
  • [x] The code has been formatted using Black (black --fast homeassistant tests)
  • [x] 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:

  • [x] 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.

To help with the load of incoming pull requests:

Shutgun avatar Jan 31 '23 16:01 Shutgun

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!

Code owner commands

Code owners of devolo_home_network can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Change the title of the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign devolo_home_network Removes the current integration label and assignees on the issue, add the integration domain after the command.

home-assistant[bot] avatar Jan 31 '23 16:01 home-assistant[bot]

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[bot] avatar Nov 22 '23 00:11 github-actions[bot]

It's still waiting for review.

Shutgun avatar Nov 22 '23 05:11 Shutgun

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Nov 23 '23 08:11 home-assistant[bot]

I need help with the mypy error. For whatever reason, I'm not seeing it locally...

Shutgun avatar Nov 24 '23 10:11 Shutgun

I need help with the mypy error. For whatever reason, I'm not seeing it locally...

Maybe you need to update mypy?

$ mypy --version
mypy 1.7.1 (compiled: yes)

If that's not it, try wiping the mypy cache.

Anyhow, the problem is that in the base class you use a single generic data type _DataT, but in the specialized class DevoloPlcDataRateSensorEntity, you have two different types, one for the coordinator data and one for the entity descriptions.

You can fix it like this, but with better names and maybe narrower types for the two type vars:

diff --git a/homeassistant/components/devolo_home_network/sensor.py b/homeassistant/components/devolo_home_network/sensor.py
index 3eb57c84764..b09a904228b 100644
--- a/homeassistant/components/devolo_home_network/sensor.py
+++ b/homeassistant/components/devolo_home_network/sensor.py
@@ -36,6 +36,10 @@ _DataT = TypeVar(
     "_DataT",
     bound=LogicalNetwork | DataRate | list[ConnectedStationInfo] | list[NeighborAPInfo],
 )
+_Data2T = TypeVar(
+    "_Data2T",
+    bound=LogicalNetwork | DataRate | list[ConnectedStationInfo] | list[NeighborAPInfo],
+)


 class DataRateDirection(StrEnum):
@@ -112,10 +116,10 @@ async def async_setup_entry(
         entry.entry_id
     ]["coordinators"]

-    entities: list[DevoloSensorEntity[Any]] = []
+    entities: list[BaseDevoloSensorEntity[Any, Any]] = []
     if device.plcnet:
         entities.append(
-            DevoloSensorEntity(
+            BaseDevoloSensorEntity(
                 entry,
                 coordinators[CONNECTED_PLC_DEVICES],
                 SENSOR_TYPES[CONNECTED_PLC_DEVICES],
@@ -165,29 +169,33 @@ async def async_setup_entry(
     async_add_entities(entities)


-class DevoloSensorEntity(DevoloCoordinatorEntity[_DataT], SensorEntity):
-    """Representation of a devolo sensor."""
-
-    entity_description: DevoloSensorEntityDescription[_DataT]
-
+class BaseDevoloSensorEntity(
+    Generic[_DataT, _Data2T], DevoloCoordinatorEntity[_DataT], SensorEntity
+):
     def __init__(
         self,
         entry: ConfigEntry,
         coordinator: DataUpdateCoordinator[_DataT],
-        description: DevoloSensorEntityDescription[_DataT],
+        description: DevoloSensorEntityDescription[_Data2T],
         device: Device,
     ) -> None:
         """Initialize entity."""
         self.entity_description = description
         super().__init__(entry, coordinator, device)

+
+class DevoloSensorEntity(BaseDevoloSensorEntity[_DataT, _DataT]):
+    """Representation of a devolo sensor."""
+
+    entity_description: DevoloSensorEntityDescription[_DataT]
+
     @property
     def native_value(self) -> float:
         """State of the sensor."""
         return self.entity_description.value_func(self.coordinator.data)


-class DevoloPlcDataRateSensorEntity(DevoloSensorEntity[DataRate]):
+class DevoloPlcDataRateSensorEntity(BaseDevoloSensorEntity[LogicalNetwork, DataRate]):
     """Representation of a devolo PLC data rate sensor."""

     entity_description: DevoloSensorEntityDescription[DataRate]

emontnemery avatar Dec 14 '23 15:12 emontnemery

@Shutgun please mark the PR ready for review when you've fixed the typing errors

emontnemery avatar Dec 15 '23 07:12 emontnemery

thx for your help, @emontnemery

Shutgun avatar Dec 18 '23 11:12 Shutgun