From bf6913182bbb26eddf6b38ee01415fd29b0bc4bb Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 3 Jul 2023 11:43:43 +0000 Subject: [PATCH 1/3] Address comments --- homeassistant/components/opower/sensor.py | 22 +++++++++++++++++--- homeassistant/components/opower/strings.json | 2 +- tests/components/opower/conftest.py | 2 -- tests/components/opower/test_config_flow.py | 2 ++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index e28dcbd0661a75..e6986e05ee5bb6 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -13,8 +13,9 @@ SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import UnitOfEnergy, UnitOfVolume +from homeassistant.const import CURRENCY_DOLLAR, 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 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_cost_to_date", name="Current bill electric cost to date", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.cost_to_date, @@ -79,6 +82,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_forecasted_cost", name="Current bill electric forecasted cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.forecasted_cost, @@ -87,6 +92,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_typical_cost", name="Typical monthly electric cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.typical_cost, @@ -127,6 +134,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_cost_to_date", name="Current bill gas cost to date", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.cost_to_date, @@ -135,6 +144,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_forecasted_cost", name="Current bill gas forecasted cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, state_class=SensorStateClass.TOTAL, suggested_display_precision=0, value_fn=lambda data: data.forecasted_cost, @@ -143,6 +154,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_typical_cost", name="Typical monthly gas cost", device_class=SensorDeviceClass.MONETARY, + native_unit_of_measurement=CURRENCY_DOLLAR, + suggested_unit_of_measurement=CURRENCY_DOLLAR, 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 @@ def __init__( ) -> 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 79d8bf80feeb37..037983eb6ffd49 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 17c6896593b030..0ee910f84f4be4 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 7f6a847f52e3b4..6a45a0dcc561a3 100644 --- a/tests/components/opower/test_config_flow.py +++ b/tests/components/opower/test_config_flow.py @@ -8,6 +8,7 @@ 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() From a64208d34d3693e957386ed86e99ffea1df6b2d1 Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 3 Jul 2023 05:32:50 -0700 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Martin Hjelmare --- homeassistant/components/opower/sensor.py | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index e6986e05ee5bb6..60e569c7230d76 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -72,8 +72,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_cost_to_date", name="Current bill electric cost to date", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, @@ -82,8 +82,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_forecasted_cost", name="Current bill electric forecasted cost", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, @@ -92,8 +92,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="elec_typical_cost", name="Typical monthly electric cost", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, @@ -134,8 +134,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_cost_to_date", name="Current bill gas cost to date", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, @@ -144,8 +144,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_forecasted_cost", name="Current bill gas forecasted cost", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, @@ -154,8 +154,8 @@ class OpowerEntityDescription(SensorEntityDescription, OpowerEntityDescriptionMi key="gas_typical_cost", name="Typical monthly gas cost", device_class=SensorDeviceClass.MONETARY, - native_unit_of_measurement=CURRENCY_DOLLAR, - suggested_unit_of_measurement=CURRENCY_DOLLAR, + 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, From d5e6d5ca5087346352c5ec3c6f270afc47f72380 Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 3 Jul 2023 05:33:14 -0700 Subject: [PATCH 3/3] Update sensor.py --- homeassistant/components/opower/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index 60e569c7230d76..ef8d8eb884fd73 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -13,7 +13,7 @@ SensorStateClass, ) from homeassistant.config_entries import ConfigEntry -from homeassistant.const import CURRENCY_DOLLAR, UnitOfEnergy, UnitOfVolume +from homeassistant.const import UnitOfEnergy, UnitOfVolume from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType from homeassistant.helpers.entity import DeviceInfo