Skip to content

Commit

Permalink
Deprconst (#81)
Browse files Browse the repository at this point in the history
* Use UnitOfPower.WATT instead of POWER_WATT

* Use UnitOfElectricCurrent.AMPERE instead of ELECTRIC_CURRENT_AMPERE

* Use UnitOfElectricPotential.VOLT instead of ELECTRIC_POTENTIAL_VOLT

* Fix imports

* Use UnitOfReactivePower.VOLT_AMPERE_REACTIVE instead of POWER_VOLT_AMPERE_REACTIVE

* Config updates
  • Loading branch information
toreamun authored Dec 23, 2024
1 parent c385f02 commit 80946e5
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ coverage.xml
.storage
automations.yaml
blueprints
configuration.yaml
config/*
!config/configuration.yaml
deps
home-assistant_v2*
home-assistant.log*
Expand Down
16 changes: 16 additions & 0 deletions config/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# https://www.home-assistant.io/integrations/default_config/
default_config:

# https://www.home-assistant.io/integrations/homeassistant/
homeassistant:
debug: true

# https://www.home-assistant.io/integrations/logger/
logger:
default: info
logs:
custom_components.amshan: debug

debugpy:
start: true
wait: true
13 changes: 13 additions & 0 deletions configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
debug: true
5 changes: 3 additions & 2 deletions custom_components/amshan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from han import common as han_type, meter_connection, obis_map
from homeassistant import const as ha_const
from homeassistant.const import UnitOfReactivePower
from homeassistant.components import sensor as ha_sensor
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import CALLBACK_TYPE, callback, HomeAssistant, Event
Expand Down Expand Up @@ -262,12 +263,12 @@ def _migrate_entity_entry_from_v2_to_v3(entity: entity_registry.RegistryEntry):
obis_map.FIELD_REACTIVE_POWER_EXPORT,
):
update["device_class"] = ha_sensor.SensorDeviceClass.REACTIVE_POWER
update["unit_of_measurement"] = ha_const.POWER_VOLT_AMPERE_REACTIVE
update["unit_of_measurement"] = UnitOfReactivePower.VOLT_AMPERE_REACTIVE
_LOGGER.info(
"Migrated %s to device class %s with unit %s",
entity.unique_id,
ha_sensor.SensorDeviceClass.REACTIVE_POWER,
ha_const.POWER_VOLT_AMPERE_REACTIVE,
UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
)

break
Expand Down
25 changes: 13 additions & 12 deletions custom_components/amshan/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from han import autodecoder, common as han_type, obis_map
from homeassistant import const as ha_const
from homeassistant.const import UnitOfPower, UnitOfEnergy, UnitOfElectricCurrent, UnitOfElectricPotential, UnitOfReactivePower
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
Expand Down Expand Up @@ -90,7 +91,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_ACTIVE_POWER_IMPORT,
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=ha_const.POWER_WATT,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_POWER_IMPORT,
name="Active power import (Q1+Q4)",
Expand All @@ -100,7 +101,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_ACTIVE_POWER_EXPORT,
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=ha_const.POWER_WATT,
native_unit_of_measurement=UnitOfPower.WATT,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_POWER_EXPORT,
name="Active power export (Q2+Q3)",
Expand All @@ -110,7 +111,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_REACTIVE_POWER_IMPORT,
device_class=SensorDeviceClass.REACTIVE_POWER,
native_unit_of_measurement=ha_const.POWER_VOLT_AMPERE_REACTIVE,
native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_POWER_IMPORT,
name="Reactive power import (Q1+Q2)",
Expand All @@ -120,7 +121,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_REACTIVE_POWER_EXPORT,
device_class=SensorDeviceClass.REACTIVE_POWER,
native_unit_of_measurement=ha_const.POWER_VOLT_AMPERE_REACTIVE,
native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_POWER_EXPORT,
name="Reactive power export (Q3+Q4)",
Expand All @@ -130,7 +131,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_CURRENT_L1,
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=ha_const.ELECTRIC_CURRENT_AMPERE,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_CURRENT,
name="Current phase L1",
Expand All @@ -140,7 +141,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_CURRENT_L2,
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=ha_const.ELECTRIC_CURRENT_AMPERE,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
name="Current phase L2",
decimals=3,
Expand All @@ -149,7 +150,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_CURRENT_L3,
device_class=SensorDeviceClass.CURRENT,
native_unit_of_measurement=ha_const.ELECTRIC_CURRENT_AMPERE,
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
state_class=SensorStateClass.MEASUREMENT,
name="Current phase L3",
decimals=3,
Expand All @@ -158,7 +159,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_VOLTAGE_L1,
device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ha_const.ELECTRIC_POTENTIAL_VOLT,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_VOLTAGE,
name="Phase L1 voltage",
Expand All @@ -168,7 +169,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_VOLTAGE_L2,
device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ha_const.ELECTRIC_POTENTIAL_VOLT,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_VOLTAGE,
name="Phase L2 voltage",
Expand All @@ -178,7 +179,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_VOLTAGE_L3,
device_class=SensorDeviceClass.VOLTAGE,
native_unit_of_measurement=ha_const.ELECTRIC_POTENTIAL_VOLT,
native_unit_of_measurement=UnitOfElectricPotential.VOLT,
state_class=SensorStateClass.MEASUREMENT,
icon=ICON_VOLTAGE,
name="Phase L3 voltage",
Expand All @@ -188,7 +189,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_ACTIVE_POWER_IMPORT_TOTAL,
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ha_const.ENERGY_KILO_WATT_HOUR,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon=ICON_COUNTER,
name="Cumulative hourly active import energy (A+) (Q1+Q4)",
Expand All @@ -200,7 +201,7 @@ class AmsHanSensorEntityDescription(SensorEntityDescription):
AmsHanSensorEntityDescription(
key=obis_map.FIELD_ACTIVE_POWER_EXPORT_TOTAL,
device_class=SensorDeviceClass.ENERGY,
native_unit_of_measurement=ha_const.ENERGY_KILO_WATT_HOUR,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
state_class=SensorStateClass.TOTAL_INCREASING,
icon=ICON_COUNTER,
name="Cumulative hourly active export energy (A-) (Q2+Q3)",
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pip>=21.0,<23.1
pip>=21.3.1
colorlog
homeassistant
14 changes: 13 additions & 1 deletion scripts/develop
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,17 @@ set -e

cd "$(dirname "$0")/.."

# Create config dir if not present
if [[ ! -d "${PWD}/config" ]]; then
mkdir -p "${PWD}/config"
hass --config "${PWD}/config" --script ensure_config
fi

# Set the path to custom_components
## This let's us have the structure we want <root>/custom_components/amshan
## while at the same time have Home Assistant configuration inside <root>/config
## without resulting to symlinks.
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"

# Start Home Assistant
hass -c . --debug
hass --config "${PWD}/config" --debug

0 comments on commit 80946e5

Please sign in to comment.