Skip to content

Commit

Permalink
Merge be8d9f7 into 1757dec
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 2, 2023
2 parents 1757dec + be8d9f7 commit 9ad7d3c
Show file tree
Hide file tree
Showing 27 changed files with 362 additions and 275 deletions.
19 changes: 19 additions & 0 deletions custom_components/powercalc/__init__.py
Expand Up @@ -46,6 +46,7 @@
CONF_POWER_SENSOR_PRECISION,
CONF_POWER_TEMPLATE,
CONF_SENSOR_TYPE,
CONF_SENSORS,
CONF_UNAVAILABLE_POWER,
CONF_UTILITY_METER_OFFSET,
CONF_UTILITY_METER_TARIFFS,
Expand Down Expand Up @@ -75,6 +76,7 @@
UnitPrefix,
)
from .discovery import DiscoveryManager
from .sensor import SENSOR_CONFIG
from .sensors.group import (
remove_group_from_power_sensor_entry,
remove_power_sensor_from_associated_groups,
Expand Down Expand Up @@ -159,6 +161,10 @@
),
vol.Optional(CONF_IGNORE_UNAVAILABLE_STATE): cv.boolean,
vol.Optional(CONF_UNAVAILABLE_POWER): vol.Coerce(float),
vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list,
[SENSOR_CONFIG],
),
},
),
),
Expand Down Expand Up @@ -214,6 +220,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
discovery_manager = DiscoveryManager(hass, config)
await discovery_manager.start_discovery()

sensors: list = domain_config.get(CONF_SENSORS, [])
for sensor_config in sensors:
sensor_config.update({DISCOVERY_TYPE: PowercalcDiscoveryType.USER_YAML})
hass.async_create_task(
async_load_platform(
hass,
Platform.SENSOR,
DOMAIN,
sensor_config,
config,
),
)

domain_groups: list[str] | None = domain_config.get(CONF_CREATE_DOMAIN_GROUPS)
if domain_groups:

Expand Down
2 changes: 2 additions & 0 deletions custom_components/powercalc/const.py
Expand Up @@ -81,6 +81,7 @@
CONF_ON_TIME = "on_time"
CONF_TEMPLATE = "template"
CONF_SENSOR_TYPE = "sensor_type"
CONF_SENSORS = "sensors"
CONF_SUB_PROFILE = "sub_profile"
CONF_SLEEP_POWER = "sleep_power"
CONF_UNAVAILABLE_POWER = "unavailable_power"
Expand Down Expand Up @@ -186,3 +187,4 @@ class PowercalcDiscoveryType(StrEnum):
DOMAIN_GROUP = "domain_group"
STANDBY_GROUP = "standby_group"
LIBRARY = "library"
USER_YAML = "user_yaml"
10 changes: 9 additions & 1 deletion custom_components/powercalc/discovery.py
Expand Up @@ -21,6 +21,7 @@
CONF_MANUFACTURER,
CONF_MODE,
CONF_MODEL,
CONF_SENSORS,
DISCOVERY_POWER_PROFILE,
DISCOVERY_SOURCE_ENTITY,
DISCOVERY_TYPE,
Expand Down Expand Up @@ -265,7 +266,7 @@ def _load_manually_configured_entities(self) -> list[str]:
"""Looks at the YAML and GUI config entries for all the configured entity_id's."""
entities = []

# Find entity ids in yaml config
# Find entity ids in yaml config (Legacy)
if SENSOR_DOMAIN in self.ha_config:
sensor_config = self.ha_config.get(SENSOR_DOMAIN)
platform_entries = [
Expand All @@ -276,6 +277,13 @@ def _load_manually_configured_entities(self) -> list[str]:
for entry in platform_entries:
entities.extend(self._find_entity_ids_in_yaml_config(entry))

# Find entity ids in yaml config (New)
domain_config: ConfigType = self.ha_config.get(DOMAIN, {})
if CONF_SENSORS in domain_config:
sensors = domain_config[CONF_SENSORS]
for sensor_config in sensors:
entities.extend(self._find_entity_ids_in_yaml_config(sensor_config))

# Add entities from existing config entries
entities.extend(
[
Expand Down
21 changes: 21 additions & 0 deletions custom_components/powercalc/sensor.py
Expand Up @@ -33,6 +33,7 @@
EVENT_ENTITY_REGISTRY_UPDATED,
RegistryEntryDisabler,
)
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

Expand Down Expand Up @@ -254,6 +255,26 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Setup sensors from YAML config sensor entries."""

# Legacy sensor platform config is used. Raise an issue.
if not discovery_info and config:
async_create_issue(
hass,
DOMAIN,
"powercalc_deprecated_yaml",
breaks_in_ha_version=None,
is_fixable=False,
severity=IssueSeverity.WARNING,
learn_more_url="https://homeassistant-powercalc.readthedocs.io/en/latest/configuration/new-yaml-configuration.yaml",
translation_key="deprecated_platform_yaml",
translation_placeholders={"platform": SENSOR_DOMAIN},
)

# Support new YAML configuration structure. powercalc -> sensors.
if discovery_info and discovery_info.get(DISCOVERY_TYPE) == PowercalcDiscoveryType.USER_YAML:
config = discovery_info
discovery_info = None

await _async_setup_entities(
hass,
config,
Expand Down
6 changes: 6 additions & 0 deletions custom_components/powercalc/strings.json
Expand Up @@ -223,5 +223,11 @@
"group_mandatory": "[%key:component::powercalc::config::error::group_mandatory%]",
"unknown": "[%key:component::powercalc::config::error::unknown%]"
}
},
"issues": {
"deprecated_platform_yaml": {
"title": "Powercalc YAML configuration has moved",
"description": "Configuring sensors using `sensor->platform` has been deprecated. You need to change your configuration to `powercalc->sensors`. Click on 'Learn more' for further instructions."
}
}
}
6 changes: 6 additions & 0 deletions custom_components/powercalc/translations/en.json
Expand Up @@ -223,5 +223,11 @@
}
}
}
},
"issues": {
"deprecated_platform_yaml": {
"title": "Powercalc YAML configuration has moved",
"description": "Configuring sensors using `sensor->platform` has been deprecated. You need to change your configuration to `powercalc->sensors`. Click on 'Learn more' for further instructions."
}
}
}
6 changes: 6 additions & 0 deletions custom_components/powercalc/translations/nl.json
Expand Up @@ -223,5 +223,11 @@
}
}
}
},
"issues": {
"deprecated_platform_yaml": {
"title": "Powercalc YAML configuratie is verplaatst",
"description": "Configuratie middels `sensor->platform` is verouderd. Verander je configratie naar `powercalc->sensors`. Klik op 'Meer informatie' voor verder uitleg."
}
}
}
16 changes: 9 additions & 7 deletions docs/source/configuration/multiply-factor.rst
Expand Up @@ -12,20 +12,22 @@ Let's assume you have a combination of 4 GU10 spots in your ceiling in a light g

.. code-block:: yaml
- platform: powercalc
entity_id: light.livingroom_spots
multiply_factor: 4
powercalc:
sensors:
- entity_id: light.livingroom_spots
multiply_factor: 4
This will add the power sensor `sensor.livingroom_spots_power` and the measured power will be multiplied by 4, as the original measurements are for 1 spot.

By default the multiply factor will **NOT** be applied to the standby power, you can set the `multiply_factor_standby` to do this.

.. code-block:: yaml
- platform: powercalc
entity_id: light.livingroom_spots
multiply_factor: 4
multiply_factor_standby: true
powercalc:
sensors:
- entity_id: light.livingroom_spots
multiply_factor: 4
multiply_factor_standby: true
.. tip::
a multiply_factor lower than 1 will decrease the power. For example 0.5 will half the power.
38 changes: 38 additions & 0 deletions docs/source/configuration/new-yaml-structure.rst
@@ -0,0 +1,38 @@
==================
New YAML structure
==================

In Powercalc 1.8.0 the structure of YAML has been changed.
This has been done to stay in line with other HA components and ensure Powercalc keeps working correctly in the future.
The old configuration format will stay working for some time, but I'll remove support in the future. So it's recommended to adopt your configuration. It's a small change.

Old configuration:

.. code-block:: yaml
sensor:
- platform: powercalc
entity_id: light.some_light
fixed:
power: 50
- platform: powercalc
entity_id: light.other_light
fixed:
power: 80
New configuration. Everything should move under the `powercalc->sensors` key, and `platform` needs to be removed.

.. code-block:: yaml
powercalc:
sensors:
- entity_id: light.some_light
fixed:
power: 50
- entity_id: light.other_light
fixed:
power: 80
.. note::
You probably already have a `powercalc:` entry in your configuration for global powercalc configuration. You'll need to add `sensors` under that to prevent duplicating the `powercalc:` key.
When you use https://www.home-assistant.io/docs/configuration/packages/ you can of course have powercalc sensor definitions scattered accross multiple files.
24 changes: 12 additions & 12 deletions docs/source/daily-fixed-energy.rst
Expand Up @@ -27,23 +27,23 @@ This will add 0.05 kWh per day to the energy sensor called "IP camera upstairs"

.. code-block:: yaml
sensor:
- platform: powercalc
name: IP camera upstairs
daily_fixed_energy:
value: 0.05
powercalc:
sensors:
- name: IP camera upstairs
daily_fixed_energy:
value: 0.05
Or define in watts, with an optional on time (which is 24 hour a day by default).

.. code-block:: yaml
sensor:
- platform: powercalc
name: Intercom
daily_fixed_energy:
value: 21
unit_of_measurement: W
on_time: 12:00:00
powercalc:
sensors:
- name: Intercom
daily_fixed_energy:
value: 21
unit_of_measurement: W
on_time: 12:00:00
This will simulate the device using 21 watts for 12 hours a day. The energy sensor will increase by 0.252 kWh a day.

Expand Down
56 changes: 28 additions & 28 deletions docs/source/group.rst
Expand Up @@ -31,15 +31,15 @@ You can combine the ``entities`` option and ``create_group`` to group individual

.. code-block:: yaml
sensor:
- platform: powercalc
create_group: All hallway lights
entities:
- entity_id: light.hallway
- entity_id: light.living_room
linear:
min_power: 0.5
max_power: 8
powercalc:
sensors:
- create_group: All hallway lights
entities:
- entity_id: light.hallway
- entity_id: light.living_room
linear:
min_power: 0.5
max_power: 8
This will create the following entities:

Expand All @@ -57,18 +57,18 @@ You can also nest groups, this makes it possible to add an entity to multiple gr

.. code-block:: yaml
sensor:
- platform: powercalc
create_group: All lights
entities:
- entity_id: light.a
- entity_id: light.b
- create_group: Upstairs lights
entities:
- entity_id: light.c
- create_group: Bedroom Bob lights
entities:
- entity_id: light.d
powercalc:
sensors:
- create_group: All lights
entities:
- entity_id: light.a
- entity_id: light.b
- create_group: Upstairs lights
entities:
- entity_id: light.c
- create_group: Bedroom Bob lights
entities:
- entity_id: light.d
Each group will have power sensors created for the following lights:

Expand Down Expand Up @@ -98,13 +98,13 @@ You can use the following configuration:

.. code-block:: yaml
sensor:
- platform: powercalc
create_group: Living Room
entities:
- power_sensor_id: sensor.heater_power
energy_sensor_id: sensor.heater_kwh
- entity_id: light.hallway #Powercalc sensor
powercalc:
sensors:
- create_group: Living Room
entities:
- power_sensor_id: sensor.heater_power
energy_sensor_id: sensor.heater_kwh
- entity_id: light.hallway #Powercalc sensor
.. note::
When you don't supply ``energy_sensor_id``, but only ``power_sensor_id`` powercalc tries to find a related energy sensor on the same device.
Expand Down

0 comments on commit 9ad7d3c

Please sign in to comment.