Add entity translations to Efergy (#95291)

* Add entity translations to Efergy

* Add entity translations to Efergy
This commit is contained in:
Joost Lekkerkerker 2023-06-27 10:22:08 +02:00 committed by GitHub
parent b12109dcde
commit ba244b7af7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 28 deletions

View file

@ -25,14 +25,14 @@ from .const import CONF_CURRENT_VALUES, DOMAIN, LOGGER
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
key="instant_readings", key="instant_readings",
name="Power Usage", translation_key="instant_readings",
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_day", key="energy_day",
name="Daily Consumption", translation_key="energy_day",
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
@ -40,7 +40,7 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_week", key="energy_week",
name="Weekly Consumption", translation_key="energy_week",
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
@ -48,14 +48,14 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_month", key="energy_month",
name="Monthly Consumption", translation_key="energy_month",
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="energy_year", key="energy_year",
name="Yearly Consumption", translation_key="energy_year",
device_class=SensorDeviceClass.ENERGY, device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
@ -63,32 +63,32 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
), ),
SensorEntityDescription( SensorEntityDescription(
key="budget", key="budget",
name="Energy Budget", translation_key="budget",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_day", key="cost_day",
name="Daily Energy Cost", translation_key="cost_day",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_week", key="cost_week",
name="Weekly Energy Cost", translation_key="cost_week",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_month", key="cost_month",
name="Monthly Energy Cost", translation_key="cost_month",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="cost_year", key="cost_year",
name="Yearly Energy Cost", translation_key="cost_year",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
@ -137,6 +137,8 @@ async def async_setup_entry(
class EfergySensor(EfergyEntity, SensorEntity): class EfergySensor(EfergyEntity, SensorEntity):
"""Implementation of an Efergy sensor.""" """Implementation of an Efergy sensor."""
_attr_has_entity_name = True
def __init__( def __init__(
self, self,
api: Efergy, api: Efergy,

View file

@ -16,5 +16,39 @@
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
} }
},
"entity": {
"sensor": {
"instant_readings": {
"name": "Power usage"
},
"energy_day": {
"name": "Daily consumption"
},
"energy_week": {
"name": "Weekly consumption"
},
"energy_month": {
"name": "Monthly consumption"
},
"energy_year": {
"name": "Yearly consumption"
},
"budget": {
"name": "Energy budget"
},
"cost_day": {
"name": "Daily energy cost"
},
"cost_week": {
"name": "Weekly energy cost"
},
"cost_month": {
"name": "Monthly energy cost"
},
"cost_year": {
"name": "Yearly energy cost"
}
}
} }
} }

View file

@ -35,62 +35,62 @@ async def test_sensor_readings(
entry = await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN) entry = await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN)
ent_reg: EntityRegistry = er.async_get(hass) ent_reg: EntityRegistry = er.async_get(hass)
state = hass.states.get("sensor.power_usage") state = hass.states.get("sensor.efergy_power_usage")
assert state.state == "1580" assert state.state == "1580"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
state = hass.states.get("sensor.energy_budget") state = hass.states.get("sensor.efergy_energy_budget")
assert state.state == "ok" assert state.state == "ok"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
assert state.attributes.get(ATTR_STATE_CLASS) is None assert state.attributes.get(ATTR_STATE_CLASS) is None
state = hass.states.get("sensor.daily_consumption") state = hass.states.get("sensor.efergy_daily_consumption")
assert state.state == "38.21" assert state.state == "38.21"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.weekly_consumption") state = hass.states.get("sensor.efergy_weekly_consumption")
assert state.state == "267.47" assert state.state == "267.47"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.monthly_consumption") state = hass.states.get("sensor.efergy_monthly_consumption")
assert state.state == "1069.88" assert state.state == "1069.88"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.yearly_consumption") state = hass.states.get("sensor.efergy_yearly_consumption")
assert state.state == "13373.50" assert state.state == "13373.50"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.ENERGY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.daily_energy_cost") state = hass.states.get("sensor.efergy_daily_energy_cost")
assert state.state == "5.27" assert state.state == "5.27"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.weekly_energy_cost") state = hass.states.get("sensor.efergy_weekly_energy_cost")
assert state.state == "36.89" assert state.state == "36.89"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.monthly_energy_cost") state = hass.states.get("sensor.efergy_monthly_energy_cost")
assert state.state == "147.56" assert state.state == "147.56"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
state = hass.states.get("sensor.yearly_energy_cost") state = hass.states.get("sensor.efergy_yearly_energy_cost")
assert state.state == "1844.50" assert state.state == "1844.50"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.MONETARY
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR" assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "EUR"
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.TOTAL_INCREASING
entity = ent_reg.async_get("sensor.power_usage_728386") entity = ent_reg.async_get("sensor.efergy_power_usage_728386")
assert entity.disabled_by is er.RegistryEntryDisabler.INTEGRATION assert entity.disabled_by is er.RegistryEntryDisabler.INTEGRATION
ent_reg.async_update_entity(entity.entity_id, **{"disabled_by": None}) ent_reg.async_update_entity(entity.entity_id, **{"disabled_by": None})
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("sensor.power_usage_728386") state = hass.states.get("sensor.efergy_power_usage_728386")
assert state.state == "1628" assert state.state == "1628"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
@ -104,17 +104,17 @@ async def test_multi_sensor_readings(
for description in SENSOR_TYPES: for description in SENSOR_TYPES:
description.entity_registry_enabled_default = True description.entity_registry_enabled_default = True
await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN, MULTI_SENSOR_TOKEN) await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN, MULTI_SENSOR_TOKEN)
state = hass.states.get("sensor.power_usage_728386") state = hass.states.get("sensor.efergy_power_usage_728386")
assert state.state == "218" assert state.state == "218"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
state = hass.states.get("sensor.power_usage_0") state = hass.states.get("sensor.efergy_power_usage_0")
assert state.state == "1808" assert state.state == "1808"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT assert state.attributes.get(ATTR_STATE_CLASS) == SensorStateClass.MEASUREMENT
state = hass.states.get("sensor.power_usage_728387") state = hass.states.get("sensor.efergy_power_usage_728387")
assert state.state == "312" assert state.state == "312"
assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER assert state.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.POWER
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfPower.WATT
@ -126,16 +126,16 @@ async def test_failed_update_and_reconnection(
) -> None: ) -> None:
"""Test failed update and reconnection.""" """Test failed update and reconnection."""
await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN) await setup_platform(hass, aioclient_mock, SENSOR_DOMAIN)
assert hass.states.get("sensor.power_usage").state == "1580" assert hass.states.get("sensor.efergy_power_usage").state == "1580"
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
await mock_responses(hass, aioclient_mock, error=True) await mock_responses(hass, aioclient_mock, error=True)
next_update = dt_util.utcnow() + timedelta(seconds=30) next_update = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("sensor.power_usage").state == STATE_UNAVAILABLE assert hass.states.get("sensor.efergy_power_usage").state == STATE_UNAVAILABLE
aioclient_mock.clear_requests() aioclient_mock.clear_requests()
await mock_responses(hass, aioclient_mock) await mock_responses(hass, aioclient_mock)
next_update = dt_util.utcnow() + timedelta(seconds=30) next_update = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, next_update) async_fire_time_changed(hass, next_update)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("sensor.power_usage").state == "1580" assert hass.states.get("sensor.efergy_power_usage").state == "1580"