From 984357bb7875affab9e2c7b4a50036ce54de8e9e Mon Sep 17 00:00:00 2001 From: Bram Date: Sat, 30 Jul 2022 17:37:28 +0200 Subject: [PATCH] Change scan_interval to force_update_frequency --- README.md | 2 +- custom_components/powercalc/__init__.py | 8 +++++--- custom_components/powercalc/const.py | 3 ++- custom_components/powercalc/sensors/power.py | 10 +++++----- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 74e2e26fa..5430fc4f5 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ All these settings are completely optional. You can skip this section if you don | Name | Type | Requirement | Default | Description | | ----------------------------- | ------- | ------------ | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------- | | enable_autodiscovery | boolean | **Optional** | true | Whether you want powercalc to automatically setup power sensors for supported models in your HA instance. -| scan_interval | string | **Optional** | 00:10:00 | Interval at which the sensor state is updated, even when the power value stays the same. Format HH:MM:SS | +| force_update_frequency | string | **Optional** | 00:10:00 | Interval at which the sensor state is updated, even when the power value stays the same. Format HH:MM:SS | | create_energy_sensors | boolean | **Optional** | true | Let the component automatically create energy sensors (kWh) for every power sensor | | power_sensor_naming | string | **Optional** | {} power | Change the name of the sensors. Use the `{}` placeholder for the entity name of your appliance. This will also change the entity_id of your sensor | | power_sensor_friendly_naming | string | **Optional** | | Change the friendly name of the sensors, Use `{}` placehorder for the original entity name. | diff --git a/custom_components/powercalc/__init__.py b/custom_components/powercalc/__init__.py index b15bf58d7..de4028bef 100644 --- a/custom_components/powercalc/__init__.py +++ b/custom_components/powercalc/__init__.py @@ -38,6 +38,7 @@ CONF_ENERGY_SENSOR_NAMING, CONF_ENERGY_SENSOR_PRECISION, CONF_ENERGY_SENSOR_UNIT_PREFIX, + CONF_FORCE_UPDATE_FREQUENCY, CONF_POWER_SENSOR_CATEGORY, CONF_POWER_SENSOR_FRIENDLY_NAMING, CONF_POWER_SENSOR_NAMING, @@ -56,7 +57,7 @@ DEFAULT_ENTITY_CATEGORY, DEFAULT_POWER_NAME_PATTERN, DEFAULT_POWER_SENSOR_PRECISION, - DEFAULT_SCAN_INTERVAL, + DEFAULT_UPDATE_FREQUENCY, DEFAULT_UTILITY_METER_TYPES, DISCOVERY_LIGHT_MODEL, DISCOVERY_SOURCE_ENTITY, @@ -80,10 +81,11 @@ CONFIG_SCHEMA = vol.Schema( { DOMAIN: vol.All( + cv.deprecated(CONF_SCAN_INTERVAL, replacement_key=CONF_FORCE_UPDATE_FREQUENCY), vol.Schema( { vol.Optional( - CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL + CONF_FORCE_UPDATE_FREQUENCY, default=DEFAULT_UPDATE_FREQUENCY ): cv.time_period, vol.Optional( CONF_POWER_SENSOR_NAMING, default=DEFAULT_POWER_NAME_PATTERN @@ -160,7 +162,7 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool: CONF_ENERGY_SENSOR_PRECISION: DEFAULT_ENERGY_SENSOR_PRECISION, CONF_ENERGY_SENSOR_CATEGORY: DEFAULT_ENTITY_CATEGORY, CONF_ENERGY_SENSOR_UNIT_PREFIX: UnitPrefix.KILO, - CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL, + CONF_FORCE_UPDATE_FREQUENCY: DEFAULT_UPDATE_FREQUENCY, CONF_CREATE_DOMAIN_GROUPS: [], CONF_CREATE_ENERGY_SENSORS: True, CONF_CREATE_UTILITY_METERS: False, diff --git a/custom_components/powercalc/const.py b/custom_components/powercalc/const.py index f812eb621..91215a642 100644 --- a/custom_components/powercalc/const.py +++ b/custom_components/powercalc/const.py @@ -41,6 +41,7 @@ CONF_ENERGY_SENSOR_PRECISION = "energy_sensor_precision" CONF_ENERGY_SENSOR_UNIT_PREFIX = "energy_sensor_unit_prefix" CONF_FIXED = "fixed" +CONF_FORCE_UPDATE_FREQUENCY = "force_update_frequency" CONF_GROUP = "group" CONF_GROUP_POWER_ENTITIES = "group_power_entities" CONF_GROUP_ENERGY_ENTITIES = "group_energy_entities" @@ -113,7 +114,7 @@ class UnitPrefix(StrEnum): ENTITY_CATEGORY_SYSTEM, ] -DEFAULT_SCAN_INTERVAL = timedelta(minutes=10) +DEFAULT_UPDATE_FREQUENCY = timedelta(minutes=10) DEFAULT_POWER_NAME_PATTERN = "{} power" DEFAULT_POWER_SENSOR_PRECISION = 2 DEFAULT_ENERGY_INTEGRATION_METHOD = ENERGY_INTEGRATION_METHOD_TRAPEZODIAL diff --git a/custom_components/powercalc/sensors/power.py b/custom_components/powercalc/sensors/power.py index 9082c5a74..08065c177 100644 --- a/custom_components/powercalc/sensors/power.py +++ b/custom_components/powercalc/sensors/power.py @@ -13,7 +13,6 @@ ) from homeassistant.const import ( CONF_NAME, - CONF_SCAN_INTERVAL, CONF_UNIQUE_ID, POWER_WATT, STATE_ON, @@ -52,6 +51,7 @@ CONF_POWER_SENSOR_ID, CONF_POWER_SENSOR_PRECISION, CONF_STANDBY_POWER, + CONF_FORCE_UPDATE_FREQUENCY, CONF_WLED, DATA_CALCULATOR_FACTORY, DISCOVERY_LIGHT_MODEL, @@ -200,7 +200,7 @@ async def create_virtual_power_sensor( unique_id=unique_id, standby_power=standby_power, standby_power_on=standby_power_on, - scan_interval=sensor_config.get(CONF_SCAN_INTERVAL), + update_frequency=sensor_config.get(CONF_FORCE_UPDATE_FREQUENCY), multiply_factor=sensor_config.get(CONF_MULTIPLY_FACTOR), multiply_factor_standby=sensor_config.get(CONF_MULTIPLY_FACTOR_STANDBY), ignore_unavailable_state=sensor_config.get(CONF_IGNORE_UNAVAILABLE_STATE), @@ -283,7 +283,7 @@ def __init__( unique_id: str, standby_power: Decimal, standby_power_on: Decimal, - scan_interval, + update_frequency, multiply_factor: float | None, multiply_factor_standby: bool, ignore_unavailable_state: bool, @@ -301,7 +301,7 @@ def __init__( self._standby_power_on = standby_power_on self._attr_force_update = True self._attr_unique_id = unique_id - self._scan_interval = scan_interval + self._update_frequency = update_frequency self._multiply_factor = multiply_factor self._multiply_factor_standby = multiply_factor_standby self._ignore_unavailable_state = ignore_unavailable_state @@ -370,7 +370,7 @@ def async_update(event_time=None): """Update the entity.""" self.async_schedule_update_ha_state(True) - async_track_time_interval(self.hass, async_update, self._scan_interval) + async_track_time_interval(self.hass, async_update, self._update_frequency) async def _update_power_sensor( self, trigger_entity_id: str, state: State | None