1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00

Remove deprecated unit configuration option from integration integration (#69157)

This commit is contained in:
Diogo Gomes 2022-04-13 18:19:34 +01:00 committed by GitHub
parent 77efe385b7
commit fa28ee1f14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 19 deletions

View File

@ -65,7 +65,7 @@ UNIT_TIME = {
DEFAULT_ROUND = 3
PLATFORM_SCHEMA = vol.All(
cv.deprecated(CONF_UNIT_OF_MEASUREMENT),
cv.removed(CONF_UNIT_OF_MEASUREMENT),
PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME): cv.string,
@ -74,7 +74,7 @@ PLATFORM_SCHEMA = vol.All(
vol.Optional(CONF_ROUND_DIGITS, default=DEFAULT_ROUND): vol.Coerce(int),
vol.Optional(CONF_UNIT_PREFIX, default=None): vol.In(UNIT_PREFIXES),
vol.Optional(CONF_UNIT_TIME, default=TIME_HOURS): vol.In(UNIT_TIME),
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Remove(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_METHOD, default=METHOD_TRAPEZOIDAL): vol.In(
INTEGRATION_METHODS
),
@ -105,7 +105,6 @@ async def async_setup_entry(
round_digits=int(config_entry.options[CONF_ROUND_DIGITS]),
source_entity=source_entity_id,
unique_id=config_entry.entry_id,
unit_of_measurement=None,
unit_prefix=unit_prefix,
unit_time=config_entry.options[CONF_UNIT_TIME],
)
@ -126,7 +125,6 @@ async def async_setup_platform(
round_digits=config[CONF_ROUND_DIGITS],
source_entity=config[CONF_SOURCE_SENSOR],
unique_id=config.get(CONF_UNIQUE_ID),
unit_of_measurement=config.get(CONF_UNIT_OF_MEASUREMENT),
unit_prefix=config[CONF_UNIT_PREFIX],
unit_time=config[CONF_UNIT_TIME],
)
@ -145,7 +143,6 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
round_digits: int,
source_entity: str,
unique_id: str | None,
unit_of_measurement: str | None,
unit_prefix: str | None,
unit_time: str,
) -> None:
@ -160,7 +157,7 @@ class IntegrationSensor(RestoreEntity, SensorEntity):
self._unit_template = (
f"{'' if unit_prefix is None else unit_prefix}{{}}{unit_time}"
)
self._unit_of_measurement = unit_of_measurement
self._unit_of_measurement = None
self._unit_prefix = UNIT_PREFIXES[unit_prefix]
self._unit_time = UNIT_TIME[unit_time]
self._attr_state_class = SensorStateClass.TOTAL

View File

@ -4,8 +4,10 @@ from unittest.mock import patch
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
ENERGY_KILO_WATT_HOUR,
ENERGY_WATT_HOUR,
POWER_KILO_WATT,
POWER_WATT,
STATE_UNKNOWN,
TIME_SECONDS,
@ -24,7 +26,6 @@ async def test_state(hass) -> None:
"platform": "integration",
"name": "integration",
"source": "sensor.power",
"unit": ENERGY_KILO_WATT_HOUR,
"round": 2,
}
}
@ -34,7 +35,7 @@ async def test_state(hass) -> None:
assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 1, {})
hass.states.async_set(entity_id, 1, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT})
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
@ -45,7 +46,13 @@ async def test_state(hass) -> None:
future_now = dt_util.utcnow() + timedelta(seconds=3600)
with patch("homeassistant.util.dt.utcnow", return_value=future_now):
hass.states.async_set(
entity_id, 1, {"device_class": SensorDeviceClass.POWER}, force_update=True
entity_id,
1,
{
"device_class": SensorDeviceClass.POWER,
ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT,
},
force_update=True,
)
await hass.async_block_till_done()
@ -137,7 +144,6 @@ async def test_trapezoidal(hass):
"platform": "integration",
"name": "integration",
"source": "sensor.power",
"unit": ENERGY_KILO_WATT_HOUR,
"round": 2,
}
}
@ -152,7 +158,12 @@ async def test_trapezoidal(hass):
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT},
force_update=True,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
@ -171,7 +182,6 @@ async def test_left(hass):
"name": "integration",
"method": "left",
"source": "sensor.power",
"unit": ENERGY_KILO_WATT_HOUR,
"round": 2,
}
}
@ -179,14 +189,19 @@ async def test_left(hass):
assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 0, {})
hass.states.async_set(entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT})
await hass.async_block_till_done()
# Testing a power sensor with non-monotonic intervals and values
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT},
force_update=True,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
@ -205,7 +220,6 @@ async def test_right(hass):
"name": "integration",
"method": "right",
"source": "sensor.power",
"unit": ENERGY_KILO_WATT_HOUR,
"round": 2,
}
}
@ -213,14 +227,19 @@ async def test_right(hass):
assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 0, {})
hass.states.async_set(entity_id, 0, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT})
await hass.async_block_till_done()
# Testing a power sensor with non-monotonic intervals and values
for time, value in [(20, 10), (30, 30), (40, 5), (50, 0)]:
now = dt_util.utcnow() + timedelta(minutes=time)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, value, {}, force_update=True)
hass.states.async_set(
entity_id,
value,
{ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT},
force_update=True,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")
@ -280,12 +299,17 @@ async def test_suffix(hass):
assert await async_setup_component(hass, "sensor", config)
entity_id = config["sensor"]["source"]
hass.states.async_set(entity_id, 1000, {})
hass.states.async_set(entity_id, 1000, {ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT})
await hass.async_block_till_done()
now = dt_util.utcnow() + timedelta(seconds=10)
with patch("homeassistant.util.dt.utcnow", return_value=now):
hass.states.async_set(entity_id, 1000, {}, force_update=True)
hass.states.async_set(
entity_id,
1000,
{ATTR_UNIT_OF_MEASUREMENT: POWER_KILO_WATT},
force_update=True,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.integration")