Skip to content

Commit

Permalink
Add and use currency constants (#40113)
Browse files Browse the repository at this point in the history
  • Loading branch information
springstan authored Sep 15, 2020
1 parent 90f5b17 commit 000d204
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 19 deletions.
21 changes: 11 additions & 10 deletions homeassistant/components/dsmr_reader/definitions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Definitions for DSMR Reader sensors added to MQTT."""

from homeassistant.const import (
CURRENCY_EURO,
ELECTRICAL_CURRENT_AMPERE,
ENERGY_KILO_WATT_HOUR,
VOLT,
Expand Down Expand Up @@ -166,17 +167,17 @@ def tariff_transform(value):
"dsmr/day-consumption/electricity1_cost": {
"name": "Low tariff cost",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/electricity2_cost": {
"name": "High tariff cost",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/electricity_cost_merged": {
"name": "Power total cost",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/gas": {
"name": "Gas usage",
Expand All @@ -186,37 +187,37 @@ def tariff_transform(value):
"dsmr/day-consumption/gas_cost": {
"name": "Gas cost",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/total_cost": {
"name": "Total cost",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/energy_supplier_price_electricity_delivered_1": {
"name": "Low tariff delivered price",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/energy_supplier_price_electricity_delivered_2": {
"name": "High tariff delivered price",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/energy_supplier_price_electricity_returned_1": {
"name": "Low tariff returned price",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/energy_supplier_price_electricity_returned_2": {
"name": "High tariff returned price",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/day-consumption/energy_supplier_price_gas": {
"name": "Gas price",
"icon": "mdi:currency-eur",
"unit": "€",
"unit": CURRENCY_EURO,
},
"dsmr/meter-stats/dsmr_version": {
"name": "DSMR version",
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/growatt_server/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
CURRENCY_EURO,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
Expand Down Expand Up @@ -39,8 +40,8 @@
# Sensor type order is: Sensor name, Unit of measurement, api data name, additional options

TOTAL_SENSOR_TYPES = {
"total_money_today": ("Total money today", "€", "plantMoneyText", {}),
"total_money_total": ("Money lifetime", "€", "totalMoneyText", {}),
"total_money_today": ("Total money today", CURRENCY_EURO, "plantMoneyText", {}),
"total_money_total": ("Money lifetime", CURRENCY_EURO, "totalMoneyText", {}),
"total_energy_today": (
"Energy Today",
ENERGY_KILO_WATT_HOUR,
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/isy994/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from homeassistant.const import (
AREA_SQUARE_METERS,
CONCENTRATION_PARTS_PER_MILLION,
CURRENCY_DOLLAR,
DEGREE,
ENERGY_KILO_WATT_HOUR,
FREQUENCY_HERTZ,
Expand Down Expand Up @@ -397,7 +398,7 @@
UOM_8_BIT_RANGE: "", # Range 0-255, no unit.
UOM_DOUBLE_TEMP: UOM_DOUBLE_TEMP,
"102": "kWs",
"103": "$",
"103": CURRENCY_DOLLAR,
"104": "¢",
"105": LENGTH_INCHES,
"106": f"mm/{TIME_DAYS}",
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/pvpc_hourly_pricing/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from aiopvpc import PVPCData

from homeassistant import config_entries
from homeassistant.const import CONF_NAME, ENERGY_KILO_WATT_HOUR
from homeassistant.const import CONF_NAME, CURRENCY_EURO, ENERGY_KILO_WATT_HOUR
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.event import async_call_later, async_track_time_change
Expand All @@ -19,7 +19,7 @@

ATTR_PRICE = "price"
ICON = "mdi:currency-eur"
UNIT = f"/{ENERGY_KILO_WATT_HOUR}"
UNIT = f"{CURRENCY_EURO}/{ENERGY_KILO_WATT_HOUR}"

_DEFAULT_TIMEOUT = 10

Expand Down
9 changes: 7 additions & 2 deletions homeassistant/components/tankerkoenig/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

import logging

from homeassistant.const import ATTR_ATTRIBUTION, ATTR_LATITUDE, ATTR_LONGITUDE
from homeassistant.const import (
ATTR_ATTRIBUTION,
ATTR_LATITUDE,
ATTR_LONGITUDE,
CURRENCY_EURO,
)
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand Down Expand Up @@ -106,7 +111,7 @@ def icon(self):
@property
def unit_of_measurement(self):
"""Return unit of measurement."""
return "€"
return CURRENCY_EURO

@property
def state(self):
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@
# Degree units
DEGREE = "°"

# Currency units
CURRENCY_EURO = "€"
CURRENCY_DOLLAR = "$"

# Temperature units
TEMP_CELSIUS = f"{DEGREE}C"
TEMP_FAHRENHEIT = f"{DEGREE}F"
Expand Down
11 changes: 9 additions & 2 deletions tests/components/pvpc_hourly_pricing/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import pytest

from homeassistant.components.pvpc_hourly_pricing import ATTR_TARIFF, DOMAIN
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, ENERGY_KILO_WATT_HOUR
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
CURRENCY_EURO,
ENERGY_KILO_WATT_HOUR,
)

from tests.common import load_fixture
from tests.test_util.aiohttp import AiohttpClientMocker
Expand All @@ -15,7 +19,10 @@
def check_valid_state(state, tariff: str, value=None, key_attr=None):
"""Ensure that sensor has a valid state and attributes."""
assert state
assert state.attributes[ATTR_UNIT_OF_MEASUREMENT] == f"€/{ENERGY_KILO_WATT_HOUR}"
assert (
state.attributes[ATTR_UNIT_OF_MEASUREMENT]
== f"{CURRENCY_EURO}/{ENERGY_KILO_WATT_HOUR}"
)
try:
_ = float(state.state)
# safety margins for current electricity price (it shouldn't be out of [0, 0.2])
Expand Down

0 comments on commit 000d204

Please sign in to comment.