diff --git a/homeassistant/components/trane/climate.py b/homeassistant/components/trane/climate.py index 19fbfa7f2115..093e53e97736 100644 --- a/homeassistant/components/trane/climate.py +++ b/homeassistant/components/trane/climate.py @@ -55,7 +55,7 @@ async def async_setup_entry( """Set up Trane Local climate entities.""" conn = config_entry.runtime_data async_add_entities( - TraneClimateEntity(conn, config_entry.entry_id, zone_id) + TraneClimateEntity(hass, conn, config_entry.entry_id, zone_id) for zone_id in conn.state.zones ) @@ -76,9 +76,15 @@ class TraneClimateEntity(TraneZoneEntity, ClimateEntity): _attr_temperature_unit = UnitOfTemperature.FAHRENHEIT _attr_target_temperature_step = 1.0 - def __init__(self, conn: ThermostatConnection, entry_id: str, zone_id: str) -> None: + def __init__( + self, + hass: HomeAssistant, + conn: ThermostatConnection, + entry_id: str, + zone_id: str, + ) -> None: """Initialize the climate entity.""" - super().__init__(conn, entry_id, zone_id, "zone") + super().__init__(hass, conn, entry_id, zone_id, "zone") modes: list[HVACMode] = [] for zone_mode in conn.state.supported_modes: ha_mode = ZONE_MODE_TO_HA.get(zone_mode) diff --git a/homeassistant/components/trane/entity.py b/homeassistant/components/trane/entity.py index 830922f0a005..69fb81d5900d 100644 --- a/homeassistant/components/trane/entity.py +++ b/homeassistant/components/trane/entity.py @@ -4,7 +4,8 @@ from typing import Any, override from steamloop import ThermostatConnection, Zone -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import Entity @@ -37,6 +38,7 @@ class TraneZoneEntity(TraneEntity): def __init__( self, + hass: HomeAssistant, conn: ThermostatConnection, entry_id: str, zone_id: str, @@ -52,7 +54,9 @@ class TraneZoneEntity(TraneEntity): manufacturer=MANUFACTURER, name=zone_name, suggested_area=zone_name, - via_device=(DOMAIN, entry_id), + via_device_id=dr.async_get_device_id_by_identifier( + hass, (DOMAIN, entry_id), config_entry_id=entry_id + ), ) @property diff --git a/homeassistant/components/trane/switch.py b/homeassistant/components/trane/switch.py index 008828f02910..b70f0ff1e9b3 100644 --- a/homeassistant/components/trane/switch.py +++ b/homeassistant/components/trane/switch.py @@ -22,7 +22,7 @@ async def async_setup_entry( """Set up Trane Local switch entities.""" conn = config_entry.runtime_data async_add_entities( - TraneHoldSwitch(conn, config_entry.entry_id, zone_id) + TraneHoldSwitch(hass, conn, config_entry.entry_id, zone_id) for zone_id in conn.state.zones ) @@ -32,9 +32,15 @@ class TraneHoldSwitch(TraneZoneEntity, SwitchEntity): _attr_translation_key = "hold" - def __init__(self, conn: ThermostatConnection, entry_id: str, zone_id: str) -> None: + def __init__( + self, + hass: HomeAssistant, + conn: ThermostatConnection, + entry_id: str, + zone_id: str, + ) -> None: """Initialize the hold switch.""" - super().__init__(conn, entry_id, zone_id, "hold") + super().__init__(hass, conn, entry_id, zone_id, "hold") @property @override diff --git a/tests/components/trane/test_init.py b/tests/components/trane/test_init.py index 91ab50731d98..7318c3fb6272 100644 --- a/tests/components/trane/test_init.py +++ b/tests/components/trane/test_init.py @@ -2,10 +2,15 @@ from unittest.mock import MagicMock +import pytest from steamloop import AuthenticationError, SteamloopConnectionError +from homeassistant.components.trane.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from .conftest import MOCK_ENTRY_ID from tests.common import MockConfigEntry @@ -24,6 +29,23 @@ async def test_load_unload( assert entry.state is ConfigEntryState.NOT_LOADED +@pytest.mark.usefixtures("init_integration") +async def test_zone_device_via_device_id( + device_registry: dr.DeviceRegistry, +) -> None: + """Test the zone device links to the thermostat device via via_device_id.""" + thermostat_device = device_registry.async_get_device_by_identifier( + (DOMAIN, MOCK_ENTRY_ID), MOCK_ENTRY_ID + ) + assert thermostat_device is not None + + zone_device = device_registry.async_get_device_by_identifier( + (DOMAIN, f"{MOCK_ENTRY_ID}_1"), MOCK_ENTRY_ID + ) + assert zone_device is not None + assert zone_device.via_device_id == thermostat_device.id + + async def test_setup_connection_error( hass: HomeAssistant, mock_config_entry: MockConfigEntry,