Address Opower review comments (#95763)

* Address comments

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update sensor.py

---------

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
tronikos 2023-07-03 05:53:44 -07:00 committed by GitHub
parent bd6f70c236
commit 4e7d8b579a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 5 deletions

View file

@ -15,6 +15,7 @@ from homeassistant.components.sensor import (
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfEnergy, UnitOfVolume from homeassistant.const import UnitOfEnergy, UnitOfVolume
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType
from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
@ -71,6 +72,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="elec_cost_to_date", key="elec_cost_to_date",
name="Current bill electric cost to date", name="Current bill electric cost to date",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.cost_to_date, value_fn=lambda data: data.cost_to_date,
@ -79,6 +82,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="elec_forecasted_cost", key="elec_forecasted_cost",
name="Current bill electric forecasted cost", name="Current bill electric forecasted cost",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.forecasted_cost, value_fn=lambda data: data.forecasted_cost,
@ -87,6 +92,8 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="elec_typical_cost", key="elec_typical_cost",
name="Typical monthly electric cost", name="Typical monthly electric cost",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.typical_cost, value_fn=lambda data: data.typical_cost,
@ -127,6 +134,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="gas_cost_to_date", key="gas_cost_to_date",
name="Current bill gas cost to date", name="Current bill gas cost to date",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.cost_to_date, value_fn=lambda data: data.cost_to_date,
@ -135,6 +144,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="gas_forecasted_cost", key="gas_forecasted_cost",
name="Current bill gas forecasted cost", name="Current bill gas forecasted cost",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.forecasted_cost, value_fn=lambda data: data.forecasted_cost,
@ -143,6 +154,8 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = (
key="gas_typical_cost", key="gas_typical_cost",
name="Typical monthly gas cost", name="Typical monthly gas cost",
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
native_unit_of_measurement="USD",
suggested_unit_of_measurement="USD",
state_class=SensorStateClass.TOTAL, state_class=SensorStateClass.TOTAL,
suggested_display_precision=0, suggested_display_precision=0,
value_fn=lambda data: data.typical_cost, 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}", name=f"{forecast.account.meter_type.name} account {forecast.account.utility_account_id}",
manufacturer="Opower", manufacturer="Opower",
model=coordinator.api.utility.name(), model=coordinator.api.utility.name(),
entry_type=DeviceEntryType.SERVICE,
) )
sensors: tuple[OpowerEntityDescription, ...] = () sensors: tuple[OpowerEntityDescription, ...] = ()
if ( if (
@ -191,9 +205,11 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class OpowerSensor(SensorEntity, CoordinatorEntity[OpowerCoordinator]): class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity):
"""Representation of an Opower sensor.""" """Representation of an Opower sensor."""
entity_description: OpowerEntityDescription
def __init__( def __init__(
self, self,
coordinator: OpowerCoordinator, coordinator: OpowerCoordinator,
@ -204,7 +220,7 @@ class OpowerSensor(SensorEntity, CoordinatorEntity[OpowerCoordinator]):
) -> None: ) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.entity_description: OpowerEntityDescription = description self.entity_description = description
self._attr_unique_id = f"{device_id}_{description.key}" self._attr_unique_id = f"{device_id}_{description.key}"
self._attr_device_info = device self._attr_device_info = device
self.utility_account_id = utility_account_id self.utility_account_id = utility_account_id

View file

@ -21,7 +21,7 @@
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
}, },
"abort": { "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%]" "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]"
} }
} }

View file

@ -2,7 +2,6 @@
import pytest import pytest
from homeassistant.components.opower.const import DOMAIN from homeassistant.components.opower.const import DOMAIN
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -19,7 +18,6 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry:
"username": "test-username", "username": "test-username",
"password": "test-password", "password": "test-password",
}, },
state=ConfigEntryState.LOADED,
) )
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
return config_entry return config_entry

View file

@ -8,6 +8,7 @@ import pytest
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.opower.const import DOMAIN from homeassistant.components.opower.const import DOMAIN
from homeassistant.components.recorder import Recorder from homeassistant.components.recorder import Recorder
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType from homeassistant.data_entry_flow import FlowResultType
@ -172,6 +173,7 @@ async def test_form_valid_reauth(
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test that we can handle a valid reauth.""" """Test that we can handle a valid reauth."""
mock_config_entry.state = ConfigEntryState.LOADED
mock_config_entry.async_start_reauth(hass) mock_config_entry.async_start_reauth(hass)
await hass.async_block_till_done() await hass.async_block_till_done()