diff --git a/homeassistant/components/opentherm_gw/__init__.py b/homeassistant/components/opentherm_gw/__init__.py index 8d1d3ae4d62..cc08ec3da69 100644 --- a/homeassistant/components/opentherm_gw/__init__.py +++ b/homeassistant/components/opentherm_gw/__init__.py @@ -26,6 +26,9 @@ from homeassistant.const import ( PRECISION_WHOLE, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.device_registry import ( + async_get_registry as async_get_dev_reg, +) from homeassistant.helpers.dispatcher import async_dispatcher_send from .const import ( @@ -404,6 +407,7 @@ class OpenThermGatewayDevice: self.gw_id = config_entry.data[CONF_ID] self.name = config_entry.data[CONF_NAME] self.climate_config = config_entry.options + self.config_entry_id = config_entry.entry_id self.status = {} self.update_signal = f"{DATA_OPENTHERM_GW}_{self.gw_id}_update" self.options_update_signal = f"{DATA_OPENTHERM_GW}_{self.gw_id}_options_update" @@ -419,9 +423,22 @@ class OpenThermGatewayDevice: async def connect_and_subscribe(self): """Connect to serial device and subscribe report handler.""" self.status = await self.gateway.connect(self.hass.loop, self.device_path) - _LOGGER.debug("Connected to OpenTherm Gateway at %s", self.device_path) - self.gw_version = self.status.get(gw_vars.OTGW_BUILD) - + version_string = self.status[gw_vars.OTGW].get(gw_vars.OTGW_ABOUT) + self.gw_version = version_string[18:] if version_string else None + _LOGGER.debug( + "Connected to OpenTherm Gateway %s at %s", self.gw_version, self.device_path + ) + dev_reg = await async_get_dev_reg(self.hass) + gw_dev = dev_reg.async_get_or_create( + config_entry_id=self.config_entry_id, + identifiers={(DOMAIN, self.gw_id)}, + name=self.name, + manufacturer="Schelte Bron", + model="OpenTherm Gateway", + sw_version=self.gw_version, + ) + if gw_dev.sw_version != self.gw_version: + dev_reg.async_update_device(gw_dev.id, sw_version=self.gw_version) self.hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, self.cleanup) async def handle_report(status): diff --git a/tests/components/opentherm_gw/test_init.py b/tests/components/opentherm_gw/test_init.py new file mode 100644 index 00000000000..51d75bf0923 --- /dev/null +++ b/tests/components/opentherm_gw/test_init.py @@ -0,0 +1,73 @@ +"""Test Opentherm Gateway init.""" +from unittest.mock import patch + +from pyotgw.vars import OTGW, OTGW_ABOUT + +from homeassistant import setup +from homeassistant.components.opentherm_gw.const import DOMAIN +from homeassistant.const import CONF_DEVICE, CONF_ID, CONF_NAME + +from tests.common import MockConfigEntry, mock_device_registry + +VERSION_OLD = "4.2.5" +VERSION_NEW = "4.2.8.1" +MINIMAL_STATUS = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_OLD}"}} +MINIMAL_STATUS_UPD = {OTGW: {OTGW_ABOUT: f"OpenTherm Gateway {VERSION_NEW}"}} +MOCK_GATEWAY_ID = "mock_gateway" +MOCK_CONFIG_ENTRY = MockConfigEntry( + domain=DOMAIN, + title="Mock Gateway", + data={ + CONF_NAME: "Mock Gateway", + CONF_DEVICE: "/dev/null", + CONF_ID: MOCK_GATEWAY_ID, + }, + options={}, +) + + +async def test_device_registry_insert(hass): + """Test that the device registry is initialized correctly.""" + MOCK_CONFIG_ENTRY.add_to_hass(hass) + + with patch( + "homeassistant.components.opentherm_gw.OpenThermGatewayDevice.cleanup", + return_value=None, + ), patch("pyotgw.pyotgw.connect", return_value=MINIMAL_STATUS): + await setup.async_setup_component(hass, DOMAIN, {}) + + await hass.async_block_till_done() + + device_registry = await hass.helpers.device_registry.async_get_registry() + + gw_dev = device_registry.async_get_device( + identifiers={(DOMAIN, MOCK_GATEWAY_ID)}, connections=set() + ) + assert gw_dev.sw_version == VERSION_OLD + + +async def test_device_registry_update(hass): + """Test that the device registry is updated correctly.""" + MOCK_CONFIG_ENTRY.add_to_hass(hass) + + dev_reg = mock_device_registry(hass) + dev_reg.async_get_or_create( + config_entry_id=MOCK_CONFIG_ENTRY.entry_id, + identifiers={(DOMAIN, MOCK_GATEWAY_ID)}, + name="Mock Gateway", + manufacturer="Schelte Bron", + model="OpenTherm Gateway", + sw_version=VERSION_OLD, + ) + + with patch( + "homeassistant.components.opentherm_gw.OpenThermGatewayDevice.cleanup", + return_value=None, + ), patch("pyotgw.pyotgw.connect", return_value=MINIMAL_STATUS_UPD): + await setup.async_setup_component(hass, DOMAIN, {}) + + await hass.async_block_till_done() + gw_dev = dev_reg.async_get_device( + identifiers={(DOMAIN, MOCK_GATEWAY_ID)}, connections=set() + ) + assert gw_dev.sw_version == VERSION_NEW