Skip to content

Commit

Permalink
Improve input sensors for Shelly Uni (#375)
Browse files Browse the repository at this point in the history
* Add removal condition for pulse counter sensor

* Add analog input sensors

---------

Co-authored-by: Maciej Bieniek <[email protected]>
  • Loading branch information
bieniu and bieniu authored Feb 19, 2024
1 parent 842f3f1 commit 140153d
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions python_scripts/shellies_discovery_gen2.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@
MODEL_PM_MINI_G3 = "shellypmminig3"

SENSOR_ACTIVE_POWER = "active_power"
SENSOR_ANALOG_INPUT = "analog_input"
SENSOR_ANALOG_VALUE = "analog_value"
SENSOR_APPARENT_POWER = "apparent_power"
SENSOR_BATTERY = "battery"
SENSOR_CLOUD = "cloud"
Expand Down Expand Up @@ -297,6 +299,8 @@
TOPIC_VOLTMETER = "~status/voltmeter:{id}"

TPL_ACTION_TEMPLATE = "{{%if value_json.output%}}{action}{{%else%}}idle{{%endif%}}"
TPL_ANALOG_INPUT = "{{value_json.percent}}"
TPL_ANALOG_VALUE = "{{value_json.xpercent}}"
TPL_BATTERY = "{{value_json.battery.percent}}"
TPL_COUNTER = "{{value_json.counts.total}}"
TPL_COUNTER_VALUE = "{{value_json.counts.xtotal}}"
Expand Down Expand Up @@ -491,6 +495,10 @@
KEY_VALUE_TEMPLATE: TPL_EMETER_PHASE_CURRENT,
}
DESCRIPTION_SENSOR_COUNTER = {
ATTR_REMOVAL_CONDITION: lambda config, input_id: config.get(
f"input:{input_id}", {}
).get("type")
!= "count",
KEY_ENABLED_BY_DEFAULT: True,
KEY_NAME: "Pulse counter {input}",
KEY_STATE_CLASS: STATE_CLASS_MEASUREMENT,
Expand All @@ -508,6 +516,28 @@
KEY_STATE_TOPIC: TOPIC_INPUT,
KEY_VALUE_TEMPLATE: TPL_COUNTER_VALUE,
}
DESCRIPTION_SENSOR_ANALOG_INPUT = {
ATTR_REMOVAL_CONDITION: lambda config, input_id: config.get(
f"input:{input_id}", {}
).get("type")
!= "percent",
KEY_ENABLED_BY_DEFAULT: True,
KEY_NAME: "Analog input {input}",
KEY_STATE_CLASS: STATE_CLASS_MEASUREMENT,
KEY_STATE_TOPIC: TOPIC_INPUT,
KEY_UNIT: UNIT_PERCENT,
KEY_VALUE_TEMPLATE: TPL_ANALOG_INPUT,
}
DESCRIPTION_SENSOR_ANALOG_VALUE = {
ATTR_REMOVAL_CONDITION: lambda config, input_id: config.get(f"input:{input_id}", {})
.get("xpercent", {})
.get("expr")
is None,
KEY_ENABLED_BY_DEFAULT: True,
KEY_NAME: "Analog value {input}",
KEY_STATE_TOPIC: TOPIC_INPUT,
KEY_VALUE_TEMPLATE: TPL_ANALOG_VALUE,
}
DESCRIPTION_SENSOR_N_CURRENT = {
KEY_DEVICE_CLASS: DEVICE_CLASS_CURRENT,
KEY_ENABLED_BY_DEFAULT: False,
Expand Down Expand Up @@ -1679,6 +1709,8 @@
ATTR_INPUT_SENSORS: {
SENSOR_COUNTER: DESCRIPTION_SENSOR_COUNTER,
SENSOR_COUNTER_VALUE: DESCRIPTION_SENSOR_COUNTER_VALUE,
SENSOR_ANALOG_INPUT: DESCRIPTION_SENSOR_ANALOG_INPUT,
SENSOR_ANALOG_VALUE: DESCRIPTION_SENSOR_ANALOG_VALUE,
},
ATTR_INPUT_EVENTS: [
EVENT_BUTTON_DOWN,
Expand Down

0 comments on commit 140153d

Please sign in to comment.