Fix unique_id generation for AtwZoneSensors (#51227)

pull/53195/head
Vilppu Vuorinen 2021-06-28 13:20:40 +03:00 committed by GitHub
parent ab24d16e00
commit 4ba5a4f36e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -165,7 +165,11 @@ class AtwZoneSensor(MelDeviceSensor):
def __init__(self, api: MelCloudDevice, zone: Zone, measurement, definition): def __init__(self, api: MelCloudDevice, zone: Zone, measurement, definition):
"""Initialize the sensor.""" """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._zone = zone
self._name_slug = f"{api.name} {zone.name}" self._name_slug = f"{api.name} {zone.name}"

View File

@ -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", {})
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"