From f630faa1f4eb53f18b8956f10c52c49a9658a930 Mon Sep 17 00:00:00 2001 From: Thorjan Knudsvik Date: Tue, 14 Mar 2023 10:23:40 +0000 Subject: [PATCH] unique --- README.md | 3 +- custom_components/energyscore/manifest.json | 2 +- custom_components/energyscore/sensor.py | 2 +- tests/const.py | 21 +++++++++++ tests/test_sensor.py | 42 +++++++++++++++------ 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5ae9c2a..d906f12 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ sensor: name: Heater energy_entity: sensor.heater_energy price_entity: sensor.nordpool_electricity_price + unique_id: 23115006-9C33-4DBD-BF01-498058F61BEC ``` ### Configuration variables @@ -69,7 +70,7 @@ Attribute | Data type | Type | Description name | string | Required | Name of the integration instance. This will provide name for the sensors to use in the frontend, but can be changed later if a unique_id is provided. "Boiler" will give sensors: sensor.boiler_energyscore, sensor.boiler_cost and sensor.boiler_potential_savings. energy_entity | string | Required | A total (cumulative) energy entity, e.g. from Tibber or PowerCalc integrations or a state from a device. It can be both an entity that resets at given intervals or one that keeps increasing indefinetely. If several is available, it is recommended to choose one with high update frequency. price_entity | string | Required | TA price entity which provides the current hourly energy price as the state, e.g. from Nordpool or Tibber integrations. -unique_id | string | Optional | Unique id to be able to configure the entity in the UI. +unique_id | string | Required | Unique id to be able to configure the entity in the UI. energy_treshold | float | Optional | Energy less than the treshold (during one hour) will not contribute to the EnergyScore (default = 0). rolling_hours | int | Optional | The number of hours the EnergyScore should be calculated from (default=24, min=2, max=168). diff --git a/custom_components/energyscore/manifest.json b/custom_components/energyscore/manifest.json index 592f03a..dfc8b27 100644 --- a/custom_components/energyscore/manifest.json +++ b/custom_components/energyscore/manifest.json @@ -9,5 +9,5 @@ "integration_type": "device", "iot_class": "calculated", "issue_tracker": "https://github.com/knudsvik/energyscore/issues", - "version": "1.3.1" + "version": "1.3.2" } \ No newline at end of file diff --git a/custom_components/energyscore/sensor.py b/custom_components/energyscore/sensor.py index 55780e5..728e2e8 100644 --- a/custom_components/energyscore/sensor.py +++ b/custom_components/energyscore/sensor.py @@ -55,7 +55,7 @@ vol.Required(CONF_NAME): cv.string, vol.Required(CONF_ENERGY_ENTITY): cv.entity_id, vol.Required(CONF_PRICE_ENTITY): cv.entity_id, - vol.Optional(CONF_UNIQUE_ID): cv.string, + vol.Required(CONF_UNIQUE_ID): cv.string, vol.Optional(CONF_TRESHOLD, default=0): vol.Coerce(float), vol.Optional(CONF_ROLLING_HOURS, default=24): vol.All( int, vol.Range(min=2, max=168) diff --git a/tests/const.py b/tests/const.py index 083cc7a..717e842 100644 --- a/tests/const.py +++ b/tests/const.py @@ -19,9 +19,30 @@ "name": "My Mock ES", "energy_entity": "sensor.energy", "price_entity": "sensor.electricity_price", + "unique_id": "Testing123", } } + +VALID_CONFIG_2 = { + "sensor": [ + { + "platform": "energyscore", + "name": "My Mock ES", + "energy_entity": "sensor.energy", + "price_entity": "sensor.electricity_price", + "unique_id": "Testing123", + }, + { + "platform": "energyscore", + "name": "My Alternative ES", + "energy_entity": "sensor.alternative_energy", + "price_entity": "sensor.electricity_price", + "unique_id": "Testing456", + }, + ], +} + VALID_UI_CONFIG = { "name": "UI", "energy_entity": "sensor.energy_ui", diff --git a/tests/test_sensor.py b/tests/test_sensor.py index e2f6bf2..9ded62a 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -38,6 +38,7 @@ PRICE_DICT, SAME_PRICE_DICT, VALID_CONFIG, + VALID_CONFIG_2, TEST_PARAMS, ) @@ -47,6 +48,8 @@ async def test_new_config(hass: HomeAssistant, caplog) -> None: assert await async_setup_component(hass, "sensor", VALID_CONFIG) await hass.async_block_till_done() + entity_reg = er.async_get(hass) + # EnergyScore state = hass.states.get("sensor.my_mock_es_energyscore") assert state @@ -62,6 +65,9 @@ async def test_new_config(hass: HomeAssistant, caplog) -> None: assert state.attributes.get("last_updated") is None assert state.attributes.get("icon") == "mdi:speedometer" assert state.attributes.get("friendly_name") == "My Mock ES EnergyScore" + assert ( + entity_reg.async_get("sensor.my_mock_es_energyscore").unique_id == "Testing123" + ) # Cost sensor state = hass.states.get("sensor.my_mock_es_cost") @@ -80,6 +86,7 @@ async def test_new_config(hass: HomeAssistant, caplog) -> None: "Cannot provide unit of measurement for My Mock ES Cost since the source sensors are not available" in caplog.text ) + assert entity_reg.async_get("sensor.my_mock_es_cost").unique_id == "Testing123_cost" # Potential sensor state = hass.states.get("sensor.my_mock_es_potential_savings") @@ -98,26 +105,37 @@ async def test_new_config(hass: HomeAssistant, caplog) -> None: assert state.attributes.get("price") == {} assert state.attributes.get("quality") is None assert state.attributes.get("friendly_name") == "My Mock ES Potential Savings" + assert ( + entity_reg.async_get("sensor.my_mock_es_potential_savings").unique_id + == "Testing123_potential_savings" + ) -async def test_unique_id(hass: HomeAssistant) -> None: - """Testing a default setup with unique_id""" +async def test_multiple_config(hass: HomeAssistant) -> None: + """Tests that multiple yaml configs can be setup""" + + assert await async_setup_component(hass, "sensor", VALID_CONFIG_2) + await hass.async_block_till_done() + + assert hass.states.get("sensor.my_alternative_es_energyscore") + assert hass.states.get("sensor.my_alternative_es_cost") + assert hass.states.get("sensor.my_alternative_es_potential_savings") + + assert hass.states.get("sensor.my_mock_es_energyscore") + assert hass.states.get("sensor.my_mock_es_cost") + assert hass.states.get("sensor.my_mock_es_potential_savings") + + +async def test_unique_id(hass: HomeAssistant, caplog) -> None: + """Testing a default setup without unique_id""" CONFIG = copy.deepcopy(VALID_CONFIG) - CONFIG["sensor"]["unique_id"] = "Testing123" + CONFIG["sensor"].pop("unique_id") assert await async_setup_component(hass, "sensor", CONFIG) await hass.async_block_till_done() - entity_reg = er.async_get(hass) - assert ( - entity_reg.async_get("sensor.my_mock_es_energyscore").unique_id == "Testing123" - ) - assert entity_reg.async_get("sensor.my_mock_es_cost").unique_id == "Testing123_cost" - assert ( - entity_reg.async_get("sensor.my_mock_es_potential_savings").unique_id - == "Testing123_potential_savings" - ) + assert "required key not provided @ data['unique_id']" in caplog.text def test_normalisation() -> None: