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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unique_id generation for AtwZoneSensors #51227

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion homeassistant/components/melcloud/sensor.py
Expand Up @@ -165,7 +165,11 @@ class AtwZoneSensor(MelDeviceSensor):

def __init__(self, api: MelCloudDevice, zone: Zone, measurement, definition):
"""Initialize the sensor."""
super().__init__(api, measurement, definition)
if zone.zone_index == 1:
full_measurement = measurement
else:
full_measurement = f"{measurement}-zone-{zone.zone_index}"
super().__init__(api, full_measurement, definition)
self._zone = zone
self._name_slug = f"{api.name} {zone.name}"

Expand Down
41 changes: 41 additions & 0 deletions tests/components/melcloud/test_atw_zone_sensor.py
@@ -0,0 +1,41 @@
"""Test the MELCloud ATW zone sensor."""
from unittest.mock import patch

import pytest

from homeassistant.components.melcloud.sensor import AtwZoneSensor


@pytest.fixture
def mock_device():
"""Mock MELCloud device."""
with patch("homeassistant.components.melcloud.MelCloudDevice") as mock:
mock.name = "name"
mock.device.serial = 1234
mock.device.mac = "11:11:11:11:11:11"
yield mock


@pytest.fixture
def mock_zone_1():
"""Mock zone 1."""
with patch("pymelcloud.atw_device.Zone") as mock:
mock.zone_index = 1
yield mock


@pytest.fixture
def mock_zone_2():
"""Mock zone 2."""
with patch("pymelcloud.atw_device.Zone") as mock:
mock.zone_index = 2
yield mock


def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2):
"""Test unique id generation correctness."""
sensor_1 = AtwZoneSensor(mock_device, mock_zone_1, "room_temperature", {})
Copy link
Member

Choose a reason for hiding this comment

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

We should set up the integration and assert the entity unique_id via the entity registry. We shouldn't interact with the entity instance directly.

https://developers.home-assistant.io/docs/development_testing#writing-tests-for-integrations

assert sensor_1.unique_id == "1234-11:11:11:11:11:11-room_temperature"

sensor_2 = AtwZoneSensor(mock_device, mock_zone_2, "room_temperature", {})
assert sensor_2.unique_id == "1234-11:11:11:11:11:11-room_temperature-zone-2"