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