From 4e7d8b579a1d19986e64024a3383fe6f97c6c271 Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 3 Jul 2023 05:53:44 -0700 Subject: [PATCH] Address Opower review comments (#95763) * Address comments * Apply suggestions from code review Co-authored-by: Martin Hjelmare * Update sensor.py --------- Co-authored-by: Martin Hjelmare --- homeassistant/components/opower/sensor.py | 20 ++++++++++++++++++-- homeassistant/components/opower/strings.json | 2 +- tests/components/opower/conftest.py | 2 -- tests/components/opower/test_config_flow.py | 2 ++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index e28dcbd0661..ef8d8eb884f 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -15,6 +15,7 @@ from homeassistant.components.sensor import ( from homeassistant.config_entries import ConfigEntry from homeassistant.const import UnitOfEnergy, UnitOfVolume from homeassistant.core import HomeAssistant +from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType @@ -71,6 +72,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="elec_cost_to_date", name="Current bill electric cost to date", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.cost_to_date, @@ -79,6 +82,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="elec_forecasted_cost", name="Current bill electric forecasted cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.forecasted_cost, @@ -87,6 +92,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="elec_typical_cost", name="Typical monthly electric cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.typical_cost, @@ -127,6 +134,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="gas_cost_to_date", name="Current bill gas cost to date", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.cost_to_date, @@ -135,6 +144,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="gas_forecasted_cost", name="Current bill gas forecasted cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.forecasted_cost, @@ -143,6 +154,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( key="gas_typical_cost", name="Typical monthly gas cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement="USD", + suggested_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.typical_cost, @@ -165,6 +178,7 @@ async def async_setup_entry( name=f"{forecast.account.meter_type.name} account {forecast.account.utility_account_id}", manufacturer="Opower", model=coordinator.api.utility.name(), + entry_type=DeviceEntryType.SERVICE, ) sensors: tuple[OpowerEntityDescription, ...] = () if ( @@ -191,9 +205,11 @@ async def async_setup_entry( async_add_entities(entities) -class OpowerSensor(SensorEntity, CoordinatorEntity[OpowerCoordinator]): +class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity): """Representation of an Opower sensor.""" + entity_description: OpowerEntityDescription + def __init__( self, coordinator: OpowerCoordinator, @@ -204,7 +220,7 @@ class OpowerSensor(SensorEntity, CoordinatorEntity[OpowerCoordinator]): ) -> None: """Initialize the sensor.""" super().__init__(coordinator) - self.entity_description: OpowerEntityDescription = description + self.entity_description = description self._attr_unique_id = f"{device_id}_{description.key}" self._attr_device_info = device self.utility_account_id = utility_account_id diff --git a/homeassistant/components/opower/strings.json b/homeassistant/components/opower/strings.json index 79d8bf80fee..037983eb6ff 100644 --- a/homeassistant/components/opower/strings.json +++ b/homeassistant/components/opower/strings.json @@ -21,7 +21,7 @@ "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "already_configured": "[%key:common::config_flow::abort::already_configured_service%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } } diff --git a/tests/components/opower/conftest.py b/tests/components/opower/conftest.py index 17c6896593b..0ee910f84f4 100644 --- a/tests/components/opower/conftest.py +++ b/tests/components/opower/conftest.py @@ -2,7 +2,6 @@ import pytest from homeassistant.components.opower.const import DOMAIN -from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry @@ -19,7 +18,6 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: "username": "test-username", "password": "test-password", }, - state=ConfigEntryState.LOADED, ) config_entry.add_to_hass(hass) return config_entry diff --git a/tests/components/opower/test_config_flow.py b/tests/components/opower/test_config_flow.py index 7f6a847f52e..6a45a0dcc56 100644 --- a/tests/components/opower/test_config_flow.py +++ b/tests/components/opower/test_config_flow.py @@ -8,6 +8,7 @@ import pytest from homeassistant import config_entries from homeassistant.components.opower.const import DOMAIN from homeassistant.components.recorder import Recorder +from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType @@ -172,6 +173,7 @@ async def test_form_valid_reauth( mock_config_entry: MockConfigEntry, ) -> None: """Test that we can handle a valid reauth.""" + mock_config_entry.state = ConfigEntryState.LOADED mock_config_entry.async_start_reauth(hass) await hass.async_block_till_done()