Address beta review comments for WattTime (#56919)
parent
39d73ecc19
commit
7c805f048c
|
@ -27,16 +27,13 @@ PLATFORMS: list[str] = ["sensor"]
|
|||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up WattTime from a config entry."""
|
||||
hass.data.setdefault(DOMAIN, {DATA_COORDINATOR: {}})
|
||||
hass.data.setdefault(DOMAIN, {entry.entry_id: {DATA_COORDINATOR: {}}})
|
||||
|
||||
session = aiohttp_client.async_get_clientsession(hass)
|
||||
|
||||
try:
|
||||
client = await Client.async_login(
|
||||
entry.data[CONF_USERNAME],
|
||||
entry.data[CONF_PASSWORD],
|
||||
session=session,
|
||||
logger=LOGGER,
|
||||
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session=session
|
||||
)
|
||||
except WattTimeError as err:
|
||||
LOGGER.error("Error while authenticating with WattTime: %s", err)
|
||||
|
@ -62,7 +59,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
)
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id] = coordinator
|
||||
hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR] = coordinator
|
||||
|
||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||
|
||||
|
@ -73,6 +70,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN][DATA_COORDINATOR].pop(entry.entry_id)
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
|
|
@ -118,16 +118,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
step_id="location", data_schema=STEP_LOCATION_DATA_SCHEMA
|
||||
)
|
||||
|
||||
if user_input[CONF_LOCATION_TYPE] == LOCATION_TYPE_COORDINATES:
|
||||
return self.async_show_form(
|
||||
step_id="coordinates", data_schema=STEP_COORDINATES_DATA_SCHEMA
|
||||
if user_input[CONF_LOCATION_TYPE] == LOCATION_TYPE_HOME:
|
||||
return await self.async_step_coordinates(
|
||||
{
|
||||
CONF_LATITUDE: self.hass.config.latitude,
|
||||
CONF_LONGITUDE: self.hass.config.longitude,
|
||||
}
|
||||
)
|
||||
return await self.async_step_coordinates(
|
||||
{
|
||||
CONF_LATITUDE: self.hass.config.latitude,
|
||||
CONF_LONGITUDE: self.hass.config.longitude,
|
||||
}
|
||||
)
|
||||
return await self.async_step_coordinates()
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
|
|
|
@ -6,10 +6,6 @@
|
|||
"requirements": [
|
||||
"aiowatttime==0.1.1"
|
||||
],
|
||||
"ssdp": [],
|
||||
"zeroconf": [],
|
||||
"homekit": {},
|
||||
"dependencies": [],
|
||||
"codeowners": [
|
||||
"@bachya"
|
||||
],
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
"""Support for WattTime sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
|
@ -36,40 +35,24 @@ ATTR_BALANCING_AUTHORITY = "balancing_authority"
|
|||
|
||||
DEFAULT_ATTRIBUTION = "Pickup data provided by WattTime"
|
||||
|
||||
SENSOR_TYPE_REALTIME_EMISSIONS_MOER = "realtime_emissions_moer"
|
||||
SENSOR_TYPE_REALTIME_EMISSIONS_PERCENT = "realtime_emissions_percent"
|
||||
|
||||
|
||||
@dataclass
|
||||
class RealtimeEmissionsSensorDescriptionMixin:
|
||||
"""Define an entity description mixin for realtime emissions sensors."""
|
||||
|
||||
data_key: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class RealtimeEmissionsSensorEntityDescription(
|
||||
SensorEntityDescription, RealtimeEmissionsSensorDescriptionMixin
|
||||
):
|
||||
"""Describe a realtime emissions sensor."""
|
||||
SENSOR_TYPE_REALTIME_EMISSIONS_MOER = "moer"
|
||||
SENSOR_TYPE_REALTIME_EMISSIONS_PERCENT = "percent"
|
||||
|
||||
|
||||
REALTIME_EMISSIONS_SENSOR_DESCRIPTIONS = (
|
||||
RealtimeEmissionsSensorEntityDescription(
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TYPE_REALTIME_EMISSIONS_MOER,
|
||||
name="Marginal Operating Emissions Rate",
|
||||
icon="mdi:blur",
|
||||
native_unit_of_measurement=f"{MASS_POUNDS} CO2/MWh",
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
data_key="moer",
|
||||
),
|
||||
RealtimeEmissionsSensorEntityDescription(
|
||||
SensorEntityDescription(
|
||||
key=SENSOR_TYPE_REALTIME_EMISSIONS_PERCENT,
|
||||
name="Relative Marginal Emissions Intensity",
|
||||
icon="mdi:blur",
|
||||
native_unit_of_measurement=PERCENTAGE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
data_key="percent",
|
||||
),
|
||||
)
|
||||
|
||||
|
@ -78,12 +61,12 @@ async def async_setup_entry(
|
|||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up WattTime sensors based on a config entry."""
|
||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
||||
async_add_entities(
|
||||
[
|
||||
RealtimeEmissionsSensor(coordinator, description)
|
||||
for description in REALTIME_EMISSIONS_SENSOR_DESCRIPTIONS
|
||||
if description.data_key in coordinator.data
|
||||
if description.key in coordinator.data
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -91,12 +74,10 @@ async def async_setup_entry(
|
|||
class RealtimeEmissionsSensor(CoordinatorEntity, SensorEntity):
|
||||
"""Define a realtime emissions sensor."""
|
||||
|
||||
entity_description: RealtimeEmissionsSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: DataUpdateCoordinator,
|
||||
description: RealtimeEmissionsSensorEntityDescription,
|
||||
description: SensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
|
@ -119,4 +100,4 @@ class RealtimeEmissionsSensor(CoordinatorEntity, SensorEntity):
|
|||
@property
|
||||
def native_value(self) -> StateType:
|
||||
"""Return the value reported by the sensor."""
|
||||
return self.coordinator.data[self.entity_description.data_key]
|
||||
return self.coordinator.data[self.entity_description.key]
|
||||
|
|
Loading…
Reference in New Issue