Remove currency from energy, use core config (#53615)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
babd9f048f
commit
4df56bad9e
5 changed files with 12 additions and 15 deletions
|
@ -92,7 +92,6 @@ class DeviceConsumption(TypedDict):
|
||||||
class EnergyPreferences(TypedDict):
|
class EnergyPreferences(TypedDict):
|
||||||
"""Dictionary holding the energy data."""
|
"""Dictionary holding the energy data."""
|
||||||
|
|
||||||
currency: str
|
|
||||||
energy_sources: list[SourceType]
|
energy_sources: list[SourceType]
|
||||||
device_consumption: list[DeviceConsumption]
|
device_consumption: list[DeviceConsumption]
|
||||||
|
|
||||||
|
@ -230,7 +229,6 @@ class EnergyManager:
|
||||||
def default_preferences() -> EnergyPreferences:
|
def default_preferences() -> EnergyPreferences:
|
||||||
"""Return default preferences."""
|
"""Return default preferences."""
|
||||||
return {
|
return {
|
||||||
"currency": "€",
|
|
||||||
"energy_sources": [],
|
"energy_sources": [],
|
||||||
"device_consumption": [],
|
"device_consumption": [],
|
||||||
}
|
}
|
||||||
|
@ -243,7 +241,6 @@ class EnergyManager:
|
||||||
data = self.data.copy()
|
data = self.data.copy()
|
||||||
|
|
||||||
for key in (
|
for key in (
|
||||||
"currency",
|
|
||||||
"energy_sources",
|
"energy_sources",
|
||||||
"device_consumption",
|
"device_consumption",
|
||||||
):
|
):
|
||||||
|
|
|
@ -124,7 +124,6 @@ async def _process_manager_data(
|
||||||
|
|
||||||
current_entities[key] = EnergyCostSensor(
|
current_entities[key] = EnergyCostSensor(
|
||||||
adapter,
|
adapter,
|
||||||
manager.data["currency"],
|
|
||||||
untyped_flow,
|
untyped_flow,
|
||||||
)
|
)
|
||||||
to_add.append(current_entities[key])
|
to_add.append(current_entities[key])
|
||||||
|
@ -142,7 +141,6 @@ class EnergyCostSensor(SensorEntity):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
adapter: FlowAdapter,
|
adapter: FlowAdapter,
|
||||||
currency: str,
|
|
||||||
flow: dict,
|
flow: dict,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
|
@ -152,7 +150,6 @@ class EnergyCostSensor(SensorEntity):
|
||||||
self.entity_id = f"{flow[adapter.entity_energy_key]}_{adapter.entity_id_suffix}"
|
self.entity_id = f"{flow[adapter.entity_energy_key]}_{adapter.entity_id_suffix}"
|
||||||
self._attr_device_class = DEVICE_CLASS_MONETARY
|
self._attr_device_class = DEVICE_CLASS_MONETARY
|
||||||
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
self._attr_state_class = STATE_CLASS_MEASUREMENT
|
||||||
self._attr_unit_of_measurement = currency
|
|
||||||
self._flow = flow
|
self._flow = flow
|
||||||
self._last_energy_sensor_state: State | None = None
|
self._last_energy_sensor_state: State | None = None
|
||||||
|
|
||||||
|
@ -256,3 +253,8 @@ class EnergyCostSensor(SensorEntity):
|
||||||
def update_config(self, flow: dict) -> None:
|
def update_config(self, flow: dict) -> None:
|
||||||
"""Update the config."""
|
"""Update the config."""
|
||||||
self._flow = flow
|
self._flow = flow
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self) -> str | None:
|
||||||
|
"""Return the units of measurement."""
|
||||||
|
return self.hass.config.currency
|
||||||
|
|
|
@ -82,7 +82,6 @@ def ws_get_prefs(
|
||||||
@websocket_api.websocket_command(
|
@websocket_api.websocket_command(
|
||||||
{
|
{
|
||||||
vol.Required("type"): "energy/save_prefs",
|
vol.Required("type"): "energy/save_prefs",
|
||||||
vol.Optional("currency"): str,
|
|
||||||
vol.Optional("energy_sources"): ENERGY_SOURCE_SCHEMA,
|
vol.Optional("energy_sources"): ENERGY_SOURCE_SCHEMA,
|
||||||
vol.Optional("device_consumption"): [DEVICE_CONSUMPTION_SCHEMA],
|
vol.Optional("device_consumption"): [DEVICE_CONSUMPTION_SCHEMA],
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ async def test_cost_sensor_price_entity(
|
||||||
if initial_cost != "unknown":
|
if initial_cost != "unknown":
|
||||||
assert state.attributes[ATTR_LAST_RESET] == now.isoformat()
|
assert state.attributes[ATTR_LAST_RESET] == now.isoformat()
|
||||||
assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT
|
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
|
# Optional late setup of dependent entities
|
||||||
if initial_energy is None:
|
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_DEVICE_CLASS] == DEVICE_CLASS_MONETARY
|
||||||
assert state.attributes[ATTR_LAST_RESET] == now.isoformat()
|
assert state.attributes[ATTR_LAST_RESET] == now.isoformat()
|
||||||
assert state.attributes[ATTR_STATE_CLASS] == STATE_CLASS_MEASUREMENT
|
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
|
# # Unique ID temp disabled
|
||||||
# # entity_registry = er.async_get(hass)
|
# # 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})
|
hass.states.async_set(usage_sensor_entity_id, "10", {"last_reset": last_reset})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get(cost_sensor_entity_id)
|
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
|
# Nothing happens when price changes
|
||||||
if price_entity is not None:
|
if price_entity is not None:
|
||||||
|
@ -186,13 +186,13 @@ async def test_cost_sensor_price_entity(
|
||||||
msg = await client.receive_json()
|
msg = await client.receive_json()
|
||||||
assert msg["success"]
|
assert msg["success"]
|
||||||
state = hass.states.get(cost_sensor_entity_id)
|
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
|
# Additional consumption is using the new price
|
||||||
hass.states.async_set(usage_sensor_entity_id, "14.5", {"last_reset": last_reset})
|
hass.states.async_set(usage_sensor_entity_id, "14.5", {"last_reset": last_reset})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get(cost_sensor_entity_id)
|
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
|
# Check generated statistics
|
||||||
await async_wait_recording_done_without_instance(hass)
|
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})
|
hass.states.async_set(usage_sensor_entity_id, "4", {"last_reset": last_reset})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get(cost_sensor_entity_id)
|
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
|
# Energy use bumped to 10 kWh
|
||||||
hass.states.async_set(usage_sensor_entity_id, "10", {"last_reset": last_reset})
|
hass.states.async_set(usage_sensor_entity_id, "10", {"last_reset": last_reset})
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
state = hass.states.get(cost_sensor_entity_id)
|
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
|
# Check generated statistics
|
||||||
await async_wait_recording_done_without_instance(hass)
|
await async_wait_recording_done_without_instance(hass)
|
||||||
|
|
|
@ -59,7 +59,6 @@ async def test_save_preferences(hass, hass_ws_client, hass_storage) -> None:
|
||||||
assert msg["result"] == default_prefs
|
assert msg["result"] == default_prefs
|
||||||
|
|
||||||
new_prefs = {
|
new_prefs = {
|
||||||
"currency": "$",
|
|
||||||
"energy_sources": [
|
"energy_sources": [
|
||||||
{
|
{
|
||||||
"type": "grid",
|
"type": "grid",
|
||||||
|
|
Loading…
Add table
Reference in a new issue