From 4df56bad9e16d144b7fdfbebb956163a4fdb2247 Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Wed, 28 Jul 2021 17:49:55 +0200 Subject: [PATCH] Remove currency from energy, use core config (#53615) Co-authored-by: Paulus Schoutsen --- homeassistant/components/energy/data.py | 3 --- homeassistant/components/energy/sensor.py | 8 +++++--- homeassistant/components/energy/websocket_api.py | 1 - tests/components/energy/test_sensor.py | 14 +++++++------- tests/components/energy/test_websocket_api.py | 1 - 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/energy/data.py b/homeassistant/components/energy/data.py index 7e867e36bfc..c053dea4741 100644 --- a/homeassistant/components/energy/data.py +++ b/homeassistant/components/energy/data.py @@ -92,7 +92,6 @@ class DeviceConsumption(TypedDict): class EnergyPreferences(TypedDict): """Dictionary holding the energy data.""" - currency: str energy_sources: list[SourceType] device_consumption: list[DeviceConsumption] @@ -230,7 +229,6 @@ class EnergyManager: def default_preferences() -> EnergyPreferences: """Return default preferences.""" return { - "currency": "€", "energy_sources": [], "device_consumption": [], } @@ -243,7 +241,6 @@ class EnergyManager: data = self.data.copy() for key in ( - "currency", "energy_sources", "device_consumption", ): diff --git a/homeassistant/components/energy/sensor.py b/homeassistant/components/energy/sensor.py index 1a4dd4b7e41..773abbbe6b9 100644 --- a/homeassistant/components/energy/sensor.py +++ b/homeassistant/components/energy/sensor.py @@ -124,7 +124,6 @@ async def _process_manager_data( current_entities[key] = EnergyCostSensor( adapter, - manager.data["currency"], untyped_flow, ) to_add.append(current_entities[key]) @@ -142,7 +141,6 @@ class EnergyCostSensor(SensorEntity): def __init__( self, adapter: FlowAdapter, - currency: str, flow: dict, ) -> None: """Initialize the sensor.""" @@ -152,7 +150,6 @@ class EnergyCostSensor(SensorEntity): self.entity_id = f"{flow[adapter.entity_energy_key]}_{adapter.entity_id_suffix}" self._attr_device_class = DEVICE_CLASS_MONETARY self._attr_state_class = STATE_CLASS_MEASUREMENT - self._attr_unit_of_measurement = currency self._flow = flow self._last_energy_sensor_state: State | None = None @@ -256,3 +253,8 @@ class EnergyCostSensor(SensorEntity): def update_config(self, flow: dict) -> None: """Update the config.""" self._flow = flow + + @property + def unit_of_measurement(self) -> str | None: + """Return the units of measurement.""" + return self.hass.config.currency diff --git a/homeassistant/components/energy/websocket_api.py b/homeassistant/components/energy/websocket_api.py index 53e4aa7714c..d1c8869a1c2 100644 --- a/homeassistant/components/energy/websocket_api.py +++ b/homeassistant/components/energy/websocket_api.py @@ -82,7 +82,6 @@ def ws_get_prefs( @websocket_api.websocket_command( { vol.Required("type"): "energy/save_prefs", - vol.Optional("currency"): str, vol.Optional("energy_sources"): ENERGY_SOURCE_SCHEMA, vol.Optional("device_consumption"): [DEVICE_CONSUMPTION_SCHEMA], } diff --git a/tests/components/energy/test_sensor.py b/tests/components/energy/test_sensor.py index b6e0fc77188..f3af93c06c1 100644 --- a/tests/components/energy/test_sensor.py +++ b/tests/components/energy/test_sensor.py @@ -146,7 +146,7 @@ async def test_cost_sensor_price_entity( if initial_cost != "unknown": assert state.attributes[ATTR_LAST_RESET] == now.isoformat() assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "€" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "EUR" # Optional late setup of dependent entities if initial_energy is None: @@ -161,7 +161,7 @@ async def test_cost_sensor_price_entity( assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_MONETARY assert state.attributes[ATTR_LAST_RESET] == now.isoformat() assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT - assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "€" + assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == "EUR" # # Unique ID temp disabled # # entity_registry = er.async_get(hass) @@ -172,7 +172,7 @@ async def test_cost_sensor_price_entity( hass.states.async_set(usage_sensor_entity_id, "10", {"last_reset": last_reset}) await hass.async_block_till_done() state = hass.states.get(cost_sensor_entity_id) - assert state.state == "10.0" # 0 € + (10-0) kWh * 1 €/kWh = 10 € + assert state.state == "10.0" # 0 EUR + (10-0) kWh * 1 EUR/kWh = 10 EUR # Nothing happens when price changes if price_entity is not None: @@ -186,13 +186,13 @@ async def test_cost_sensor_price_entity( msg = await client.receive_json() assert msg["success"] state = hass.states.get(cost_sensor_entity_id) - assert state.state == "10.0" # 10 € + (10-10) kWh * 2 €/kWh = 10 € + assert state.state == "10.0" # 10 EUR + (10-10) kWh * 2 EUR/kWh = 10 EUR # Additional consumption is using the new price hass.states.async_set(usage_sensor_entity_id, "14.5", {"last_reset": last_reset}) await hass.async_block_till_done() state = hass.states.get(cost_sensor_entity_id) - assert state.state == "19.0" # 10 € + (14.5-10) kWh * 2 €/kWh = 19 € + assert state.state == "19.0" # 10 EUR + (14.5-10) kWh * 2 EUR/kWh = 19 EUR # Check generated statistics await async_wait_recording_done_without_instance(hass) @@ -205,13 +205,13 @@ async def test_cost_sensor_price_entity( hass.states.async_set(usage_sensor_entity_id, "4", {"last_reset": last_reset}) await hass.async_block_till_done() state = hass.states.get(cost_sensor_entity_id) - assert state.state == "0.0" # 0 € + (4-4) kWh * 2 €/kWh = 0 € + assert state.state == "0.0" # 0 EUR + (4-4) kWh * 2 EUR/kWh = 0 EUR # Energy use bumped to 10 kWh hass.states.async_set(usage_sensor_entity_id, "10", {"last_reset": last_reset}) await hass.async_block_till_done() state = hass.states.get(cost_sensor_entity_id) - assert state.state == "12.0" # 0 € + (10-4) kWh * 2 €/kWh = 12 € + assert state.state == "12.0" # 0 EUR + (10-4) kWh * 2 EUR/kWh = 12 EUR # Check generated statistics await async_wait_recording_done_without_instance(hass) diff --git a/tests/components/energy/test_websocket_api.py b/tests/components/energy/test_websocket_api.py index a37850dd566..80ede3a7548 100644 --- a/tests/components/energy/test_websocket_api.py +++ b/tests/components/energy/test_websocket_api.py @@ -59,7 +59,6 @@ async def test_save_preferences(hass, hass_ws_client, hass_storage) -> None: assert msg["result"] == default_prefs new_prefs = { - "currency": "$", "energy_sources": [ { "type": "grid",