From 14a64ea970493eb792ca6e48697b751623bf8252 Mon Sep 17 00:00:00 2001
From: Czapla <56671347+Antoni-Czaplicki@users.noreply.github.com>
Date: Thu, 11 Feb 2021 20:46:58 +0100
Subject: [PATCH] Add generic_thermostat unique ID parameter (#46399)

* Add generic_thermostat unique ID parameter

* Add tests for unique id

* Fix flake8
---
 .../components/generic_thermostat/climate.py  | 11 ++++++++
 .../generic_thermostat/test_climate.py        | 27 +++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/homeassistant/components/generic_thermostat/climate.py b/homeassistant/components/generic_thermostat/climate.py
index 433e91104ad..5fbdf499146 100644
--- a/homeassistant/components/generic_thermostat/climate.py
+++ b/homeassistant/components/generic_thermostat/climate.py
@@ -23,6 +23,7 @@ from homeassistant.const import (
     ATTR_ENTITY_ID,
     ATTR_TEMPERATURE,
     CONF_NAME,
+    CONF_UNIQUE_ID,
     EVENT_HOMEASSISTANT_START,
     PRECISION_HALVES,
     PRECISION_TENTHS,
@@ -85,6 +86,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
         vol.Optional(CONF_PRECISION): vol.In(
             [PRECISION_TENTHS, PRECISION_HALVES, PRECISION_WHOLE]
         ),
+        vol.Optional(CONF_UNIQUE_ID): cv.string,
     }
 )
 
@@ -109,6 +111,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
     away_temp = config.get(CONF_AWAY_TEMP)
     precision = config.get(CONF_PRECISION)
     unit = hass.config.units.temperature_unit
+    unique_id = config.get(CONF_UNIQUE_ID)
 
     async_add_entities(
         [
@@ -128,6 +131,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
                 away_temp,
                 precision,
                 unit,
+                unique_id,
             )
         ]
     )
@@ -153,6 +157,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
         away_temp,
         precision,
         unit,
+        unique_id,
     ):
         """Initialize the thermostat."""
         self._name = name
@@ -177,6 +182,7 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
         self._max_temp = max_temp
         self._target_temp = target_temp
         self._unit = unit
+        self._unique_id = unique_id
         self._support_flags = SUPPORT_FLAGS
         if away_temp:
             self._support_flags = SUPPORT_FLAGS | SUPPORT_PRESET_MODE
@@ -269,6 +275,11 @@ class GenericThermostat(ClimateEntity, RestoreEntity):
         """Return the name of the thermostat."""
         return self._name
 
+    @property
+    def unique_id(self):
+        """Return the unique id of this thermostat."""
+        return self._unique_id
+
     @property
     def precision(self):
         """Return the precision of the system."""
diff --git a/tests/components/generic_thermostat/test_climate.py b/tests/components/generic_thermostat/test_climate.py
index 201ed0130ff..e6cdf962d24 100644
--- a/tests/components/generic_thermostat/test_climate.py
+++ b/tests/components/generic_thermostat/test_climate.py
@@ -159,6 +159,33 @@ async def test_heater_switch(hass, setup_comp_1):
     assert STATE_ON == hass.states.get(heater_switch).state
 
 
+async def test_unique_id(hass, setup_comp_1):
+    """Test heater switching input_boolean."""
+    unique_id = "some_unique_id"
+    _setup_sensor(hass, 18)
+    _setup_switch(hass, True)
+    assert await async_setup_component(
+        hass,
+        DOMAIN,
+        {
+            "climate": {
+                "platform": "generic_thermostat",
+                "name": "test",
+                "heater": ENT_SWITCH,
+                "target_sensor": ENT_SENSOR,
+                "unique_id": unique_id,
+            }
+        },
+    )
+    await hass.async_block_till_done()
+
+    entity_registry = await hass.helpers.entity_registry.async_get_registry()
+
+    entry = entity_registry.async_get(ENTITY)
+    assert entry
+    assert entry.unique_id == unique_id
+
+
 def _setup_sensor(hass, temp):
     """Set up the test sensor."""
     hass.states.async_set(ENT_SENSOR, temp)
-- 
GitLab