Skip to content

Commit

Permalink
bug: update device and state class constants (#21)
Browse files Browse the repository at this point in the history
* refactor: use SensorDeviceClass enum

* refactor: use SensorStateClass enum

* chore: use `state_class: total_increasing` for energy sensors

* chore: use apparent and reactive power device classes

* require homeassistant==2022.2.0

Co-authored-by: IgnacioHR <[email protected]>
  • Loading branch information
luuuis and IgnacioHR authored Sep 22, 2022
1 parent c9b6b5f commit af4bd7a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 48 deletions.
42 changes: 23 additions & 19 deletions custom_components/wibeee/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
import voluptuous as vol
from homeassistant.components.sensor import (
PLATFORM_SCHEMA,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.config_entries import (ConfigEntry, SOURCE_IMPORT)
from homeassistant.const import (
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_POWER_FACTOR,
DEVICE_CLASS_VOLTAGE,
FREQUENCY_HERTZ,
POWER_WATT,
POWER_VOLT_AMPERE,
POWER_VOLT_AMPERE_REACTIVE,
ELECTRIC_POTENTIAL_VOLT,
ELECTRIC_CURRENT_AMPERE,
ENERGY_WATT_HOUR,
Expand All @@ -38,6 +34,7 @@
CONF_TIMEOUT,
CONF_UNIQUE_ID,
STATE_UNAVAILABLE,
PERCENTAGE,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import PlatformNotReady
Expand All @@ -57,6 +54,13 @@

DEFAULT_NAME = 'Wibeee Energy Consumption Sensor'

# Not known in HA yet so we do as the Fronius integration.
#
# https://github.com/home-assistant/core/blob/75abf87611d8ad5627126a3fe09fdddc8402237c/homeassistant/components/fronius/sensor.py#L43
ENERGY_VOLT_AMPERE_REACTIVE_HOUR = 'varh'

ENERGY_CLASSES = [SensorDeviceClass.ENERGY, ENERGY_VOLT_AMPERE_REACTIVE_HOUR]

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
Expand Down Expand Up @@ -86,17 +90,17 @@ class SensorType(namedtuple('SensorType', [


KNOWN_SENSORS = [
SensorType('vrms', 'v', 'Vrms', 'Phase Voltage', ELECTRIC_POTENTIAL_VOLT, DEVICE_CLASS_VOLTAGE),
SensorType('irms', 'i', 'Irms', 'Current', ELECTRIC_CURRENT_AMPERE, DEVICE_CLASS_CURRENT),
SensorType('vrms', 'v', 'Vrms', 'Phase Voltage', ELECTRIC_POTENTIAL_VOLT, SensorDeviceClass.VOLTAGE),
SensorType('irms', 'i', 'Irms', 'Current', ELECTRIC_CURRENT_AMPERE, SensorDeviceClass.CURRENT),
SensorType('frecuencia', 'q', 'Frequency', 'Frequency', FREQUENCY_HERTZ, device_class=None),
SensorType('p_activa', 'a', 'Active_Power', 'Active Power', POWER_WATT, DEVICE_CLASS_POWER),
SensorType('p_reactiva_ind', 'r', 'Inductive_Reactive_Power', 'Inductive Reactive Power', 'VArL', DEVICE_CLASS_POWER),
SensorType('p_reactiva_cap', None, 'Capacitive_Reactive_Power', 'Capacitive Reactive Power', 'VArC', DEVICE_CLASS_POWER),
SensorType('p_aparent', 'p', 'Apparent_Power', 'Apparent Power', POWER_VOLT_AMPERE, DEVICE_CLASS_POWER),
SensorType('factor_potencia', 'f', 'Power_Factor', 'Power Factor', '', DEVICE_CLASS_POWER_FACTOR),
SensorType('energia_activa', 'e', 'Active_Energy', 'Active Energy', ENERGY_WATT_HOUR, DEVICE_CLASS_ENERGY),
SensorType('energia_reactiva_ind', 'o', 'Inductive_Reactive_Energy', 'Inductive Reactive Energy', 'VArLh', DEVICE_CLASS_ENERGY),
SensorType('energia_reactiva_cap', None, 'Capacitive_Reactive_Energy', 'Capacitive Reactive Energy', 'VArCh', DEVICE_CLASS_ENERGY),
SensorType('p_activa', 'a', 'Active_Power', 'Active Power', POWER_WATT, SensorDeviceClass.POWER),
SensorType('p_reactiva_ind', 'r', 'Inductive_Reactive_Power', 'Inductive Reactive Power', POWER_VOLT_AMPERE_REACTIVE, SensorDeviceClass.REACTIVE_POWER),
SensorType('p_reactiva_cap', None, 'Capacitive_Reactive_Power', 'Capacitive Reactive Power', POWER_VOLT_AMPERE_REACTIVE, SensorDeviceClass.REACTIVE_POWER),
SensorType('p_aparent', 'p', 'Apparent_Power', 'Apparent Power', POWER_VOLT_AMPERE, SensorDeviceClass.APPARENT_POWER),
SensorType('factor_potencia', 'f', 'Power_Factor', 'Power Factor', '', SensorDeviceClass.POWER_FACTOR),
SensorType('energia_activa', 'e', 'Active_Energy', 'Active Energy', ENERGY_WATT_HOUR, SensorDeviceClass.ENERGY),
SensorType('energia_reactiva_ind', 'o', 'Inductive_Reactive_Energy', 'Inductive Reactive Energy', ENERGY_VOLT_AMPERE_REACTIVE_HOUR, SensorDeviceClass.ENERGY),
SensorType('energia_reactiva_cap', None, 'Capacitive_Reactive_Energy', 'Capacitive Reactive Energy', ENERGY_VOLT_AMPERE_REACTIVE_HOUR, SensorDeviceClass.ENERGY),
]

KNOWN_MODELS = {
Expand Down Expand Up @@ -222,7 +226,7 @@ def __init__(self, device, sensor_phase: str, sensor_type: SensorType, initial_v
self._attr_native_unit_of_measurement = sensor_type.unit
self._attr_native_value = initial_value
self._attr_available = True
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING if sensor_type.device_class is DEVICE_CLASS_ENERGY else STATE_CLASS_MEASUREMENT
self._attr_state_class = SensorStateClass.TOTAL_INCREASING if sensor_type.device_class in ENERGY_CLASSES else SensorStateClass.MEASUREMENT
self._attr_device_class = sensor_type.device_class
self._attr_unique_id = f"_{mac_addr}_{sensor_type.unique_name.lower()}_{sensor_phase}"
self._attr_name = f"{device_name} {sensor_type.friendly_name} L{sensor_phase}"
Expand Down Expand Up @@ -260,4 +264,4 @@ def _make_device_info(device, sensor_phase) -> DeviceInfo:
manufacturer='Smilics',
configuration_url=f"http://{device['ipAddr']}/" if not is_clamp else None,
sw_version=f"{device['softVersion']}" if not is_clamp else None,
)
)
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"render_readme": true,
"zip_release": true,
"filename": "hass_wibeee.zip",
"homeassistant": "2021.9.1"
"homeassistant": "2022.2.0"
}
57 changes: 29 additions & 28 deletions requirements_test.txt
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
aiohttp==3.7.4.post0
anyio==3.5.0
aiohttp==3.8.1
aiosignal==1.2.0
anyio==3.6.1
astral==2.2
async-timeout==3.0.1
async-timeout==4.0.2
atomicwrites==1.4.0
attrs==21.2.0
awesomeversion==21.4.0
awesomeversion==22.1.0
bcrypt==3.1.7
certifi==2021.10.8
cffi==1.15.0
chardet==4.0.0
charset-normalizer==2.0.11
ciso8601==2.1.3
cryptography==3.3.2
certifi==2022.9.14
cffi==1.15.1
charset-normalizer==2.0.12
ciso8601==2.2.0
cryptography==35.0.0
frozenlist==1.3.1
h11==0.12.0
homeassistant==2021.9.1
httpcore==0.13.7
httpx==0.19.0
idna==2.10
Jinja2==3.0.1
MarkupSafe==2.0.1
homeassistant==2022.2.0
httpcore==0.14.7
httpx==0.21.3
idna==3.4
ifaddr==0.1.7
Jinja2==3.0.3
MarkupSafe==2.1.1
multidict==6.0.2
pycparser==2.21
PyJWT==1.7.1
PyJWT==2.1.0
python-slugify==4.0.1
pytz==2021.3
PyYAML==5.4.1
requests==2.25.1
pytz==2022.2.1
PyYAML==6.0
requests==2.27.1
rfc3986==1.5.0
ruamel.yaml==0.15.100
six==1.16.0
sniffio==1.2.0
sniffio==1.3.0
text-unidecode==1.3
typing-extensions==4.0.1
urllib3==1.26.8
voluptuous==0.12.1
voluptuous-serialize==2.4.0
xmltodict==0.12.0
yarl==1.6.3
typing-extensions==4.3.0
urllib3==1.26.12
voluptuous==0.12.2
voluptuous-serialize==2.5.0
yarl==1.7.2

0 comments on commit af4bd7a

Please sign in to comment.