Skip to content

Commit

Permalink
Merge pull request #30 from jyavenard/HighPrecisionEnergy
Browse files Browse the repository at this point in the history
Update for Home Assistant 2021.9
  • Loading branch information
gtdiehl authored Sep 2, 2021
2 parents 38d7cda + e681e56 commit 0dbe610
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
4 changes: 0 additions & 4 deletions custom_components/iotawatt/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
"requirements": [
"iotawattpy==0.1.0"
],
"ssdp": [],
"zeroconf": [],
"homekit": {},
"dependencies": [],
"codeowners": [
"@gtdiehl"
],
Expand Down
45 changes: 24 additions & 21 deletions custom_components/iotawatt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from homeassistant.components.sensor import (
ATTR_LAST_RESET,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
SensorEntityDescription,
)

from homeassistant.const import (
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
Expand Down Expand Up @@ -56,62 +56,62 @@ class IotaWattSensorEntityDescription(SensorEntityDescription):
ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = {
"Amps": IotaWattSensorEntityDescription(
"Amps",
unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_CURRENT,
entity_registry_enabled_default=False,
),
"Hz": IotaWattSensorEntityDescription(
"Hz",
unit_of_measurement=FREQUENCY_HERTZ,
native_unit_of_measurement=FREQUENCY_HERTZ,
state_class=STATE_CLASS_MEASUREMENT,
icon="mdi:flash",
entity_registry_enabled_default=False,
),
"PF": IotaWattSensorEntityDescription(
"PF",
unit_of_measurement=PERCENTAGE,
native_unit_of_measurement=PERCENTAGE,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_POWER_FACTOR,
value=lambda value: value * 100,
entity_registry_enabled_default=False,
),
"Watts": IotaWattSensorEntityDescription(
"Watts",
unit_of_measurement=POWER_WATT,
native_unit_of_measurement=POWER_WATT,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_POWER,
),
"WattHours": IotaWattSensorEntityDescription(
"WattHours",
unit_of_measurement=ENERGY_WATT_HOUR,
native_unit_of_measurement=ENERGY_WATT_HOUR,
state_class=STATE_CLASS_TOTAL_INCREASING,
device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_MEASUREMENT,
),
"VA": IotaWattSensorEntityDescription(
"VA",
unit_of_measurement=POWER_VOLT_AMPERE,
native_unit_of_measurement=POWER_VOLT_AMPERE,
state_class=STATE_CLASS_MEASUREMENT,
icon="mdi:flash",
entity_registry_enabled_default=False,
),
"VAR": IotaWattSensorEntityDescription(
"VAR",
unit_of_measurement=VOLT_AMPERE_REACTIVE,
native_unit_of_measurement=VOLT_AMPERE_REACTIVE,
state_class=STATE_CLASS_MEASUREMENT,
icon="mdi:flash",
entity_registry_enabled_default=False,
),
"VARh": IotaWattSensorEntityDescription(
"VARh",
unit_of_measurement=VOLT_AMPERE_REACTIVE_HOURS,
native_unit_of_measurement=VOLT_AMPERE_REACTIVE_HOURS,
state_class=STATE_CLASS_MEASUREMENT,
icon="mdi:flash",
entity_registry_enabled_default=False,
),
"Volts": IotaWattSensorEntityDescription(
"Volts",
unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
state_class=STATE_CLASS_MEASUREMENT,
device_class=DEVICE_CLASS_VOLTAGE,
entity_registry_enabled_default=False,
Expand Down Expand Up @@ -172,8 +172,8 @@ def __init__(
data = self._sensor_data
self._accumulating = data.getUnit() == "WattHours" and not data.getFromStart()
self._accumulated_value = None
unit = data.getUnit() + self._name_suffix
if data.getType() == "Input":
unit = data.getUnit() + self._name_suffix
self._attr_unique_id = (
f"{data.hub_mac_address}-input-{data.getChannel()}-{unit}"
)
Expand Down Expand Up @@ -238,13 +238,16 @@ def extra_state_attributes(self):

@property
def last_reset(self):
"""Return the time when the sensor was last reset, if any."""
if self._accumulating:
return datetime.min
last_reset = self._sensor_data.getBegin()
if last_reset is None:
return None
return dt.parse_datetime(last_reset)
"""Return the time when the sensor was last reset, if any."""
if self.state_class == STATE_CLASS_MEASUREMENT:
return None

if self._accumulating:
return datetime.min # an accumulating sensor never reset.
last_reset = self._sensor_data.getBegin()
if last_reset is None:
return None
return dt.parse_datetime(last_reset)

async def async_added_to_hass(self):
"""Load the last known state value of the entity if the accumulated type."""
Expand All @@ -265,8 +268,8 @@ async def async_added_to_hass(self):
await self.coordinator.request_refresh()

@property
def state(self):
"""Return the name of the sensor."""
def native_value(self) -> entity.StateType:
"""Return the state of the sensor."""
if func := self.entity_description.value:
return func(self._sensor_data.getValue())

Expand Down
23 changes: 23 additions & 0 deletions custom_components/iotawatt/translations/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"config": {
"error": {
"cannot_connect": "Impossibile connettersi",
"invalid_auth": "Autenticazione non valida",
"unknown": "Errore imprevisto"
},
"step": {
"auth": {
"data": {
"password": "Password",
"username": "Nome utente"
},
"description": "Il dispositivo IoTawatt richiede l'autenticazione. Inserisci il nome utente e la password e fai clic sul pulsante Invia."
},
"user": {
"data": {
"host": "Host"
}
}
}
}
}
23 changes: 23 additions & 0 deletions custom_components/iotawatt/translations/no.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"config": {
"error": {
"cannot_connect": "Tilkobling mislyktes",
"invalid_auth": "Ugyldig godkjenning",
"unknown": "Uventet feil"
},
"step": {
"auth": {
"data": {
"password": "Passord",
"username": "Brukernavn"
},
"description": "IoTawatt -enheten krever autentisering. Skriv inn brukernavn og passord og klikk p\u00e5 Send -knappen."
},
"user": {
"data": {
"host": "Vert"
}
}
}
}
}
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"name": "IoTaWatt",
"render_readme": true,
"domains": ["sensor"],
"homeassistant": "2021.8.0",
"homeassistant": "2021.9.0",
"iot_class": ["Local Polling"]
}

0 comments on commit 0dbe610

Please sign in to comment.