Fix has-entity-name and entity-translations in Opower (#148098)

This commit is contained in:
tronikos 2025-07-04 01:10:21 -07:00 committed by GitHub
parent 04cc451c76
commit 8641a2141c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 94 additions and 29 deletions

View File

@ -24,6 +24,8 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import OpowerConfigEntry, OpowerCoordinator
PARALLEL_UPDATES = 0
@dataclass(frozen=True, kw_only=True)
class OpowerEntityDescription(SensorEntityDescription):
@ -38,7 +40,7 @@ class OpowerEntityDescription(SensorEntityDescription):
ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
OpowerEntityDescription(
key="elec_usage_to_date",
name="Current bill electric usage to date",
translation_key="elec_usage_to_date",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
# Not TOTAL_INCREASING because it can decrease for accounts with solar
@ -48,7 +50,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_forecasted_usage",
name="Current bill electric forecasted usage",
translation_key="elec_forecasted_usage",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL,
@ -57,7 +59,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_typical_usage",
name="Typical monthly electric usage",
translation_key="elec_typical_usage",
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL,
@ -66,7 +68,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_cost_to_date",
name="Current bill electric cost to date",
translation_key="elec_cost_to_date",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -75,7 +77,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_forecasted_cost",
name="Current bill electric forecasted cost",
translation_key="elec_forecasted_cost",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -84,7 +86,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_typical_cost",
name="Typical monthly electric cost",
translation_key="elec_typical_cost",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -93,7 +95,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_start_date",
name="Current bill electric start date",
translation_key="elec_start_date",
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
@ -101,7 +103,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="elec_end_date",
name="Current bill electric end date",
translation_key="elec_end_date",
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
@ -111,7 +113,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
OpowerEntityDescription(
key="gas_usage_to_date",
name="Current bill gas usage to date",
translation_key="gas_usage_to_date",
device_class=SensorDeviceClass.GAS,
native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
state_class=SensorStateClass.TOTAL,
@ -120,7 +122,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_forecasted_usage",
name="Current bill gas forecasted usage",
translation_key="gas_forecasted_usage",
device_class=SensorDeviceClass.GAS,
native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
state_class=SensorStateClass.TOTAL,
@ -129,7 +131,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_typical_usage",
name="Typical monthly gas usage",
translation_key="gas_typical_usage",
device_class=SensorDeviceClass.GAS,
native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET,
state_class=SensorStateClass.TOTAL,
@ -138,7 +140,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_cost_to_date",
name="Current bill gas cost to date",
translation_key="gas_cost_to_date",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -147,7 +149,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_forecasted_cost",
name="Current bill gas forecasted cost",
translation_key="gas_forecasted_cost",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -156,7 +158,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_typical_cost",
name="Typical monthly gas cost",
translation_key="gas_typical_cost",
device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL,
@ -165,7 +167,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_start_date",
name="Current bill gas start date",
translation_key="gas_start_date",
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
@ -173,7 +175,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
),
OpowerEntityDescription(
key="gas_end_date",
name="Current bill gas end date",
translation_key="gas_end_date",
device_class=SensorDeviceClass.DATE,
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
@ -229,6 +231,7 @@ async def async_setup_entry(
class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity):
"""Representation of an Opower sensor."""
_attr_has_entity_name = True
entity_description: OpowerEntityDescription
def __init__(
@ -249,8 +252,6 @@ class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity):
@property
def native_value(self) -> StateType | date:
"""Return the state."""
if self.coordinator.data is not None:
return self.entity_description.value_fn(
self.coordinator.data[self.utility_account_id]
)
return None
return self.entity_description.value_fn(
self.coordinator.data[self.utility_account_id]
)

View File

@ -37,5 +37,57 @@
"title": "Return to grid statistics for account: {utility_account_id}",
"description": "We found negative values in your existing consumption statistics, likely because you have solar. We split those in separate return statistics for a better experience in the Energy dashboard.\n\nPlease visit the [Energy configuration page]({energy_settings}) to add the following statistics in the **Return to grid** section:\n\n{target_ids}\n\nOnce you have added them, ignore this issue."
}
},
"entity": {
"sensor": {
"elec_usage_to_date": {
"name": "Current bill electric usage to date"
},
"elec_forecasted_usage": {
"name": "Current bill electric forecasted usage"
},
"elec_typical_usage": {
"name": "Typical monthly electric usage"
},
"elec_cost_to_date": {
"name": "Current bill electric cost to date"
},
"elec_forecasted_cost": {
"name": "Current bill electric forecasted cost"
},
"elec_typical_cost": {
"name": "Typical monthly electric cost"
},
"elec_start_date": {
"name": "Current bill electric start date"
},
"elec_end_date": {
"name": "Current bill electric end date"
},
"gas_usage_to_date": {
"name": "Current bill gas usage to date"
},
"gas_forecasted_usage": {
"name": "Current bill gas forecasted usage"
},
"gas_typical_usage": {
"name": "Typical monthly gas usage"
},
"gas_cost_to_date": {
"name": "Current bill gas cost to date"
},
"gas_forecasted_cost": {
"name": "Current bill gas forecasted cost"
},
"gas_typical_cost": {
"name": "Typical monthly gas cost"
},
"gas_start_date": {
"name": "Current bill gas start date"
},
"gas_end_date": {
"name": "Current bill gas end date"
}
}
}
}

View File

@ -25,36 +25,48 @@ async def test_sensors(
entity_registry = er.async_get(hass)
# Check electric sensors
entry = entity_registry.async_get("sensor.current_bill_electric_usage_to_date")
entry = entity_registry.async_get(
"sensor.elec_account_111111_current_bill_electric_usage_to_date"
)
assert entry
assert entry.unique_id == "pge_111111_elec_usage_to_date"
state = hass.states.get("sensor.current_bill_electric_usage_to_date")
state = hass.states.get(
"sensor.elec_account_111111_current_bill_electric_usage_to_date"
)
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR
assert state.state == "100"
entry = entity_registry.async_get("sensor.current_bill_electric_cost_to_date")
entry = entity_registry.async_get(
"sensor.elec_account_111111_current_bill_electric_cost_to_date"
)
assert entry
assert entry.unique_id == "pge_111111_elec_cost_to_date"
state = hass.states.get("sensor.current_bill_electric_cost_to_date")
state = hass.states.get(
"sensor.elec_account_111111_current_bill_electric_cost_to_date"
)
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "USD"
assert state.state == "20.0"
# Check gas sensors
entry = entity_registry.async_get("sensor.current_bill_gas_usage_to_date")
entry = entity_registry.async_get(
"sensor.gas_account_222222_current_bill_gas_usage_to_date"
)
assert entry
assert entry.unique_id == "pge_222222_gas_usage_to_date"
state = hass.states.get("sensor.current_bill_gas_usage_to_date")
state = hass.states.get("sensor.gas_account_222222_current_bill_gas_usage_to_date")
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS
# Convert 50 CCF to m³
assert float(state.state) == pytest.approx(50 * 2.83168, abs=1e-3)
entry = entity_registry.async_get("sensor.current_bill_gas_cost_to_date")
entry = entity_registry.async_get(
"sensor.gas_account_222222_current_bill_gas_cost_to_date"
)
assert entry
assert entry.unique_id == "pge_222222_gas_cost_to_date"
state = hass.states.get("sensor.current_bill_gas_cost_to_date")
state = hass.states.get("sensor.gas_account_222222_current_bill_gas_cost_to_date")
assert state
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "USD"
assert state.state == "15.0"