Skip to content

Commit

Permalink
2021.9 only
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Sep 8, 2021
1 parent fdcbf29 commit 7319a5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
2 changes: 1 addition & 1 deletion custom_components/erse/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.0.3",
"version": "2.1.1",
"domain": "erse",
"name": "Entidade Reguladora dos Servi\u00e7os Energ\u00e9ticos",
"config_flow": true,
Expand Down
55 changes: 16 additions & 39 deletions custom_components/erse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.components.sensor import (
ATTR_LAST_RESET,
DEVICE_CLASS_MONETARY,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.components.utility_meter.const import ATTR_TARIFF
Expand All @@ -32,8 +32,6 @@
from homeassistant.util import dt as dt_util
from homeassistant.util import slugify

from homeassistant.const import __version__ as HA_VERSION
from awesomeversion import AwesomeVersion

from .const import (
CONF_METER_SUFFIX,
Expand Down Expand Up @@ -83,12 +81,10 @@ def __init__(self, hass, entry_id, tariff, meter_entity):
self.operator = hass.data[DOMAIN][entry_id]

self._attr_device_class = DEVICE_CLASS_MONETARY
self._attr_state_class = STATE_CLASS_MEASUREMENT
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
self._attr_unit_of_measurement = (
CURRENCY_EURO # TODO _attr_native_unit_of_measurement
)
if AwesomeVersion(HA_VERSION) < "2021.9.0":
self._attr_last_reset = dt_util.utc_from_timestamp(0)

self._attr_name = f"{meter_entity} cost"
self._attr_unique_id = slugify(f"{entry_id} {meter_entity} cost")
Expand Down Expand Up @@ -122,26 +118,15 @@ async def calc_costs(meter_state):
)
kwh = 0

if AwesomeVersion(HA_VERSION) >= "2021.9.0":
self._attr_native_value = round(
self.operator.plano.custo_kWh_final(self._tariff, kwh), 2
)
_LOGGER.debug(
"{%s} calc_costs(%s) = %s",
self._attr_name,
kwh,
self._attr_native_value,
)
else:
self._attr_state = round(
self.operator.plano.custo_kWh_final(self._tariff, kwh), 2
)
_LOGGER.debug(
"{%s} calc_costs(%s) = %s",
self._attr_name,
kwh,
self._attr_state,
)
self._attr_native_value = round(
self.operator.plano.custo_kWh_final(self._tariff, kwh), 2
)
_LOGGER.debug(
"{%s} calc_costs(%s) = %s",
self._attr_name,
kwh,
self._attr_native_value,
)
self.async_write_ha_state()

@callback
Expand Down Expand Up @@ -176,11 +161,10 @@ def __init__(self, hass, entry_id, any_meter) -> None:
self.operator = hass.data[DOMAIN][entry_id]

self._attr_device_class = DEVICE_CLASS_MONETARY
self._attr_state_class = STATE_CLASS_MEASUREMENT
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
self._attr_unit_of_measurement = (
CURRENCY_EURO # TODO _attr_native_unit_of_measurement
)
self._attr_last_reset = dt_util.utc_from_timestamp(0)

self._attr_name = f"{self.operator} cost"
self._attr_unique_id = slugify(f"{entry_id} {any_meter} fixed cost")
Expand Down Expand Up @@ -213,17 +197,10 @@ async def timer_update(self, now):

elapsed = now - last_reset

if AwesomeVersion(HA_VERSION) >= "2021.9.0":
self._attr_native_value = round(
self.operator.plano.custos_fixos(elapsed.days), 2
)
_LOGGER.debug("FixedCost = %s", self._attr_native_value)
else:
if elapsed.days == 0:
self._attr_last_reset = now

self._attr_state = round(self.operator.plano.custos_fixos(elapsed.days), 2)
_LOGGER.debug("FixedCost = %s", self._attr_state)
self._attr_native_value = round(
self.operator.plano.custos_fixos(elapsed.days), 2
)
_LOGGER.debug("FixedCost = %s", self._attr_native_value)
self.async_write_ha_state()

@property
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Entidade Reguladora dos Serviços Energéticos",
"country": "PT",
"homeassistant": "2021.8.0",
"homeassistant": "2021.9.0",
"render_readme": true
}

0 comments on commit 7319a5f

Please sign in to comment.