217 files
This commit is contained in:
@@ -125,10 +125,10 @@ def create_daily_fixed_energy_sensor(
|
||||
|
||||
async def create_daily_fixed_energy_power_sensor(
|
||||
hass: HomeAssistant,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
source_entity: SourceEntity,
|
||||
) -> VirtualPowerSensor | None:
|
||||
mode_config: dict = sensor_config.get(CONF_DAILY_FIXED_ENERGY) # type: ignore
|
||||
mode_config: ConfigType = sensor_config.get(CONF_DAILY_FIXED_ENERGY) # type: ignore
|
||||
|
||||
if mode_config.get(CONF_ON_TIME) != timedelta(days=1):
|
||||
return None
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import inspect
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry, ConfigFlow
|
||||
from homeassistant.const import CONF_NAME
|
||||
@@ -84,7 +85,7 @@ async def add_to_associated_group(
|
||||
if not group_entry and len(group_entry_id) != 32:
|
||||
group_entry = hass.config_entries.async_entry_for_domain_unique_id(DOMAIN, group_entry_id)
|
||||
if not group_entry:
|
||||
additional_args: dict = {}
|
||||
additional_args: dict[str, Any] = {}
|
||||
signature = inspect.signature(ConfigEntry.__init__)
|
||||
if "discovery_keys" in signature.parameters:
|
||||
additional_args["discovery_keys"] = {}
|
||||
|
||||
@@ -48,6 +48,7 @@ from homeassistant.helpers.event import (
|
||||
from homeassistant.helpers.json import JSONEncoder
|
||||
from homeassistant.helpers.singleton import singleton
|
||||
from homeassistant.helpers.storage import Store
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from custom_components.powercalc.analytics.analytics import collect_analytics
|
||||
from custom_components.powercalc.const import (
|
||||
@@ -124,6 +125,11 @@ from custom_components.powercalc.unit import (
|
||||
ENTITY_ID_FORMAT = SENSOR_DOMAIN + ".{}"
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Predicate used to narrow a list of entities down to the group members.
|
||||
EntityPredicate = Callable[[Entity], bool]
|
||||
# Shape persisted by PreviousStateStoreStore: group id -> entity id -> serialized State.
|
||||
StoredStates = dict[str, dict[str, Any]]
|
||||
STORAGE_KEY = "powercalc_group"
|
||||
STORAGE_VERSION = 2
|
||||
# How long between periodically saving the current states to disk
|
||||
@@ -134,7 +140,7 @@ def create_group_sensors_yaml(
|
||||
hass: HomeAssistant,
|
||||
sensor_config: dict[str, Any],
|
||||
entities: list[Entity],
|
||||
filters: list[Callable] | None = None,
|
||||
filters: list[EntityPredicate] | None = None,
|
||||
) -> list[Entity]:
|
||||
"""Create grouped power and energy sensors."""
|
||||
power_sensor_ids = filter_entity_list_by_class(entities, SensorDeviceClass.POWER, filters)
|
||||
@@ -155,7 +161,7 @@ def create_group_sensors_yaml(
|
||||
async def create_group_sensors_gui(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
) -> list[Entity]:
|
||||
"""Create group sensors based on a config_entry."""
|
||||
group_name = str(entry.data.get(CONF_NAME))
|
||||
@@ -220,9 +226,9 @@ def create_group_sensors_custom(
|
||||
|
||||
|
||||
def filter_entity_list_by_class(
|
||||
all_entities: list,
|
||||
all_entities: list[Entity],
|
||||
device_class: SensorDeviceClass,
|
||||
default_filters: list[Callable] | None = None,
|
||||
default_filters: list[EntityPredicate] | None = None,
|
||||
) -> set[str]:
|
||||
"""Filter entity list to only include entities of the given class."""
|
||||
class_name = PowerSensor if device_class == SensorDeviceClass.POWER else EnergySensor
|
||||
@@ -346,7 +352,7 @@ def create_grouped_power_sensor(
|
||||
hass: HomeAssistant,
|
||||
group_name: str,
|
||||
group_type: GroupType,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
power_sensor_ids: set[str],
|
||||
) -> GroupedPowerSensor:
|
||||
name = generate_power_sensor_name(sensor_config, group_name)
|
||||
@@ -378,7 +384,7 @@ def create_grouped_energy_sensor(
|
||||
hass: HomeAssistant,
|
||||
group_name: str,
|
||||
group_type: GroupType,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
energy_sensor_ids: set[str],
|
||||
power_sensor: GroupedPowerSensor | None,
|
||||
) -> EnergySensor:
|
||||
@@ -941,7 +947,7 @@ class PreviousStateStore:
|
||||
return instance
|
||||
|
||||
def __init__(self, hass: HomeAssistant) -> None:
|
||||
self.store: Store = PreviousStateStoreStore(
|
||||
self.store: Store[StoredStates] = PreviousStateStoreStore(
|
||||
hass,
|
||||
STORAGE_VERSION,
|
||||
STORAGE_KEY,
|
||||
@@ -1007,7 +1013,7 @@ class PreviousStateStore:
|
||||
)
|
||||
|
||||
|
||||
class PreviousStateStoreStore(Store):
|
||||
class PreviousStateStoreStore(Store[StoredStates]):
|
||||
"""Store area registry data."""
|
||||
|
||||
async def _async_migrate_func( # type: ignore
|
||||
|
||||
@@ -33,7 +33,7 @@ def create_subtract_group_sensors(
|
||||
validate_config(config)
|
||||
group_name = str(config.get(CONF_NAME))
|
||||
base_entity_id = str(config.get(CONF_ENTITY_ID))
|
||||
subtract_entities = cast(list, config.get(CONF_SUBTRACT_ENTITIES))
|
||||
subtract_entities = cast(list[str], config.get(CONF_SUBTRACT_ENTITIES))
|
||||
|
||||
name = generate_power_sensor_name(config, group_name)
|
||||
unique_id = config.get(CONF_UNIQUE_ID, generate_unique_id(config))
|
||||
|
||||
@@ -31,7 +31,7 @@ from homeassistant.core import (
|
||||
State,
|
||||
callback,
|
||||
)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.exceptions import ServiceValidationError
|
||||
from homeassistant.helpers import issue_registry as ir, start
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity import EntityCategory
|
||||
@@ -118,7 +118,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
async def create_power_sensor(
|
||||
hass: HomeAssistant,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
source_entity: SourceEntity,
|
||||
config_entry: ConfigEntry | None,
|
||||
) -> PowerSensor:
|
||||
@@ -326,7 +326,7 @@ def _get_standby_power(
|
||||
|
||||
def create_real_power_sensor(
|
||||
hass: HomeAssistant,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
) -> RealPowerSensor:
|
||||
"""Create reference to an existing power sensor."""
|
||||
power_sensor_id = sensor_config.get(CONF_POWER_SENSOR_ID)
|
||||
@@ -389,7 +389,7 @@ class VirtualPowerSensor(PowerSensor, SensorEntity):
|
||||
unique_id: str | None,
|
||||
standby_power: Decimal | Template,
|
||||
standby_power_on: Decimal,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
power_profile: PowerProfile | None,
|
||||
config_entry: ConfigEntry | None,
|
||||
) -> None:
|
||||
@@ -426,7 +426,7 @@ class VirtualPowerSensor(PowerSensor, SensorEntity):
|
||||
self._sub_profile_selector: SubProfileSelector | None = None
|
||||
if not self._ignore_unavailable_state and self._sensor_config.get(CONF_UNAVAILABLE_POWER) is not None:
|
||||
self._ignore_unavailable_state = True
|
||||
self._standby_sensors: dict = hass.data[DOMAIN][DATA_STANDBY_POWER_SENSORS]
|
||||
self._standby_sensors: ConfigType = hass.data[DOMAIN][DATA_STANDBY_POWER_SENSORS]
|
||||
self.calculation_strategy_factory = calculation_strategy_factory
|
||||
self._strategy_instance: PowerCalculationStrategyInterface | None = None
|
||||
self._availability_entity: str | None = sensor_config.get(CONF_AVAILABILITY_ENTITY)
|
||||
@@ -828,7 +828,11 @@ class VirtualPowerSensor(PowerSensor, SensorEntity):
|
||||
"""Ensure we are dealing with a playbook sensor."""
|
||||
assert self._strategy_instance is not None
|
||||
if not isinstance(self._strategy_instance, PlaybookStrategy):
|
||||
raise HomeAssistantError("supported only playbook enabled sensors")
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="not_a_playbook_sensor",
|
||||
translation_placeholders={"entity_id": self.entity_id},
|
||||
)
|
||||
return self._strategy_instance
|
||||
|
||||
async def async_will_remove_from_hass(self) -> None:
|
||||
@@ -847,13 +851,22 @@ class VirtualPowerSensor(PowerSensor, SensorEntity):
|
||||
or not await self._power_profile.has_sub_profiles
|
||||
or self._power_profile.sub_profile_select
|
||||
):
|
||||
raise HomeAssistantError(
|
||||
"This is only supported for sensors having sub profiles, and no automatic profile selection",
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="no_sub_profile_support",
|
||||
translation_placeholders={"entity_id": self.entity_id},
|
||||
)
|
||||
|
||||
known_profiles = [profile[0] for profile in await self._power_profile.get_sub_profiles()]
|
||||
if profile not in known_profiles:
|
||||
raise HomeAssistantError(f"{profile} is not a possible sub profile")
|
||||
raise ServiceValidationError(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="unknown_sub_profile",
|
||||
translation_placeholders={
|
||||
"profile": profile,
|
||||
"known_profiles": ", ".join(known_profiles),
|
||||
},
|
||||
)
|
||||
|
||||
await self._select_new_sub_profile(profile)
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ GENERAL_TARIFF = "general"
|
||||
def create_utility_meters(
|
||||
hass: HomeAssistant,
|
||||
energy_sensor: EnergySensor,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
config_entry: ConfigEntry | None = None,
|
||||
) -> list[VirtualUtilityMeter]:
|
||||
"""Create the utility meters."""
|
||||
@@ -101,7 +101,7 @@ def should_create_utility_meter(
|
||||
def create_meters_for_type(
|
||||
hass: HomeAssistant,
|
||||
energy_sensor: EnergySensor,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
config_entry: ConfigEntry | None,
|
||||
unique_id: str | None,
|
||||
meter_type: str,
|
||||
@@ -153,7 +153,7 @@ def create_tariff_meters(
|
||||
energy_sensor: EnergySensor,
|
||||
entity_id: str,
|
||||
name: str,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
config_entry: ConfigEntry | None,
|
||||
meter_type: str,
|
||||
unique_id: str | None,
|
||||
@@ -183,7 +183,7 @@ def create_tariff_meters(
|
||||
|
||||
def create_tariff_select(
|
||||
config_entry: ConfigEntry | None,
|
||||
tariffs: list,
|
||||
tariffs: list[str],
|
||||
hass: HomeAssistant,
|
||||
name: str,
|
||||
unique_id: str | None,
|
||||
@@ -220,7 +220,7 @@ def create_utility_meter(
|
||||
source_entity: str,
|
||||
entity_id: str,
|
||||
name: str,
|
||||
sensor_config: dict,
|
||||
sensor_config: ConfigType,
|
||||
meter_type: str,
|
||||
unique_id: str | None = None,
|
||||
tariff: str | None = None,
|
||||
|
||||
Reference in New Issue
Block a user