Remove currency from energy, use core config (#53615)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
Bram Kragten 2021-07-28 17:49:55 +02:00 committed by GitHub
parent babd9f048f
commit 4df56bad9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 15 deletions

View file

@ -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",
):

View file

@ -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

View file

@ -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],
}

View file

@ -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)

View file

@ -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",