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

Add support for externally connected utility devices in HomeWizard #100684

Merged
merged 28 commits into from Jan 25, 2024

Conversation

DCSBL
Copy link
Contributor

@DCSBL DCSBL commented Sep 21, 2023

Breaking change

The Gas meter id sensor is removed, the meter ID is now as serial number in the device info panel.

Proposed change

Continuing from #86386. Was not able to (easily) rebase many upstream changes**

Many smart meters receive data from other meters, such as gas or water meters. So far this integration expects that exactly 1 gas meter can be connected (or nothing at all), but this is not true. In some countries water meters are connected. It is also possible to have several gas meters connected at the same time.

This change exposes all connected meters as separate devices.

Screenshot 2024-01-25 at 11 44 07

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)
  • 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:

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
  • I have followed the perfect PR recommendations
  • 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.

To help with the load of incoming pull requests:

@emontnemery emontnemery reopened this Sep 27, 2023
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
@home-assistant home-assistant bot marked this pull request as draft September 27, 2023 13:07
@home-assistant
Copy link

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

Learn more about our pull request process.

@DCSBL DCSBL marked this pull request as ready for review September 29, 2023 14:55
@wmadev
Copy link

wmadev commented Sep 29, 2023

Will this fix also work for water meters? (just checking as you only seem to talk about gas meters in the code)

Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Let's revert the gas ID / product names in the names thing.

@home-assistant home-assistant bot marked this pull request as draft September 29, 2023 17:29
homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
Comment on lines 619 to 643
class HomeWizardExternalSensorEntityIdentifier(HomeWizardEntity, SensorEntity):
"""Representation of externally connected HomeWizard Sensor."""

_attr_icon = "mdi:alphabetical-variant"
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_translation_key = "meter_identifier"

_attr_name = None

def __init__(
self,
coordinator: HWEnergyDeviceUpdateCoordinator,
device_info: DeviceInfo,
device_unique_id: str,
) -> None:
"""Initialize Externally connected HomeWizard Sensors."""
super().__init__(coordinator)
self._attr_unique_id = f"{DOMAIN}_{device_unique_id}_meter_identifier"
self._attr_native_value = device_unique_id
self._attr_device_info = device_info
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 have the feeling this can be easier.. not sure how

Comment on lines 499 to 514
device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
name=description.key,
manufacturer="HomeWizard",
model=coordinator.data.device.product_type,
)
if coordinator.data.device.serial is not None:
device_info[ATTR_VIA_DEVICE] = (
DOMAIN,
coordinator.data.device.serial,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an copy what is found in entity.py, but different. Not sure how and if we can improve that.

self._attr_device_info = DeviceInfo(
name=coordinator.entry.title,
manufacturer="HomeWizard",
sw_version=coordinator.data.device.firmware_version,
model=coordinator.data.device.product_type,
)
if coordinator.data.device.serial is not None:
self._attr_device_info[ATTR_IDENTIFIERS] = {
(DOMAIN, coordinator.data.device.serial)
}

@DCSBL DCSBL marked this pull request as ready for review October 2, 2023 14:03
@home-assistant home-assistant bot requested a review from frenck October 2, 2023 14:03
frenck
frenck previously requested changes Nov 26, 2023
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

This PR needs to be rebased onto the latest dev now

@home-assistant home-assistant bot marked this pull request as draft November 26, 2023 16:53
@emontnemery emontnemery reopened this Jan 25, 2024
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

Looks good! Just one question about dropping the meter identifier sensor and instead setting the device serial number.

homeassistant/components/homewizard/sensor.py Outdated Show resolved Hide resolved
identifiers={(DOMAIN, unique_id)},
name=description.device_name,
manufacturer="HomeWizard",
model=coordinator.data.device.product_type,
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this be enough instead of the sensor with this information?

Suggested change
model=coordinator.data.device.product_type,
model=coordinator.data.device.product_type,
serial_number=unique_id,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can remove the sensor and that will be enough IMO. But as it removes the sensor, which is migrated from the original configuration, we should mark it as breaking I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@emontnemery Is the breaking-change label still required or will it be picked up by the PR description?

async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Initialize sensors."""
coordinator: HWEnergyDeviceUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities(

# Migrate original gas meter sensor to ExternalDevice
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add code which removes the now defunct gas_unique_id sensor, otherwise users will still see it, but unavailable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Can you hint me what function to use for that or an integration that does this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, here's an example

# Remove ozone sensors from registry if they exist
ent_reg = er.async_get(hass)
for day in range(0, 5):
unique_id = f"{coordinator.location_key}-ozone-{day}"
if entity_id := ent_reg.async_get_entity_id(SENSOR_PLATFORM, DOMAIN, unique_id):
_LOGGER.debug("Removing ozone sensor entity %s", entity_id)
ent_reg.async_remove(entity_id)

Copy link
Contributor Author

@DCSBL DCSBL Jan 25, 2024

Choose a reason for hiding this comment

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

Nice, added removal and test.

@home-assistant home-assistant bot marked this pull request as draft January 25, 2024 10:53
@DCSBL DCSBL marked this pull request as ready for review January 25, 2024 11:42
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

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

This looks great, thanks @DCSBL 👍

@emontnemery emontnemery merged commit 0c9a30a into home-assistant:dev Jan 25, 2024
23 checks passed
@DCSBL DCSBL deleted the external-devices-2 branch January 25, 2024 11:52
@DCSBL
Copy link
Contributor Author

DCSBL commented Jan 25, 2024

Thanks for the quick end-sprint 👊

"""Validate unit of measurement and set device class."""
if (
self.native_unit_of_measurement
not in DEVICE_CLASS_UNITS[self._suggested_device_class]
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sold on this approch. Is there really that many different possible native units of this device per suggested device class that we can't create entity descriptions for each combination? Then we could handle special cases like m3 above already in the mapping.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is up to the device manufacturer which unit to use and how to format that. Nothing about that is specified in any standard AFAIK. The current method supports all combination that are allowed by Home Assistant.

When we specify each combination we have to collect those combinations. That is absolutely an option. In the beginning we will see some issues pop up but when most combinations are added it works the way we hope. But I think the end result is less 'clean' as it is now.

Copy link
Member

Choose a reason for hiding this comment

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

Even if we would include all allowed native units in the list of combinations it would only result in 23 items currently. That's just 18 more than what we have now. If it would become more than a couple of hundred combinations I'd understand that we don't want to list all of them.

It's always easier to read a declarative approach than a calculated approach.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 26, 2024
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.

None yet

5 participants