Skip to content

Commit

Permalink
Merge pull request #109 from knudsvik/fix_multisetup
Browse files Browse the repository at this point in the history
unique
  • Loading branch information
knudsvik authored Mar 14, 2023
2 parents b188b38 + f630faa commit 05184a3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion custom_components/energyscore/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
2 changes: 1 addition & 1 deletion custom_components/energyscore/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
42 changes: 30 additions & 12 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
PRICE_DICT,
SAME_PRICE_DICT,
VALID_CONFIG,
VALID_CONFIG_2,
TEST_PARAMS,
)

Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit 05184a3

Please sign in to comment.