Skip to content

Commit

Permalink
Merge pull request #68 from maorcc/bug-fix-3-0-1
Browse files Browse the repository at this point in the history
bug fixes
  • Loading branch information
elad-bar committed Jun 2, 2024
2 parents 2460978 + 1542e25 commit f602af3
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 74 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## v3.0.2

- Fix unload integration process
- Add last reset date for sensors (none, daily, monthly)
- Correct state_class of sensors
- Correct data of total cost per low & rate and sewage consumption
- Fix convert to string function of data models

## v3.0.1

- Add total cost per low & rate and sewage consumption
Expand Down
4 changes: 0 additions & 4 deletions custom_components/citymind_water_meter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
_LOGGER.info(f"Unloading {DOMAIN} integration, Entry ID: {entry.entry_id}")

coordinator: Coordinator = hass.data[DOMAIN][entry.entry_id]

await coordinator.terminate()

for platform in PLATFORMS:
await hass.config_entries.async_forward_entry_unload(entry, platform)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
from homeassistant.helpers.entity import EntityDescription

from .consts import UNIT_COST
from .enums import EntityKeys, EntityType
from .enums import EntityKeys, EntityType, ResetPolicy


@dataclass(frozen=True, kw_only=True)
class IntegrationEntityDescription(EntityDescription):
platform: Platform | None = None
entity_type: EntityType | None
reset_policy: ResetPolicy = ResetPolicy.NONE


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -64,56 +65,64 @@ class IntegrationNumberEntityDescription(
icon="mdi:meter-gas",
state_class=SensorStateClass.TOTAL,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.LAST_READ,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.MONTHLY_CONSUMPTION,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.TODAYS_CONSUMPTION,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.DAILY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.YESTERDAYS_CONSUMPTION,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.DAILY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.HIGH_RATE_CONSUMPTION,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.LOW_RATE_CONSUMPTION,
entity_type=EntityType.METER,
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationSensorEntityDescription(
key=EntityKeys.LOW_RATE_TOTAL_COST,
entity_type=EntityType.METER,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UNIT_COST,
icon="mdi:currency-ils",
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationNumberEntityDescription(
key=EntityKeys.LOW_RATE_COST,
Expand All @@ -130,9 +139,10 @@ class IntegrationNumberEntityDescription(
key=EntityKeys.HIGH_RATE_TOTAL_COST,
entity_type=EntityType.METER,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UNIT_COST,
icon="mdi:currency-ils",
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationNumberEntityDescription(
key=EntityKeys.HIGH_RATE_COST,
Expand All @@ -149,9 +159,10 @@ class IntegrationNumberEntityDescription(
key=EntityKeys.SEWAGE_TOTAL_COST,
entity_type=EntityType.METER,
entity_category=EntityCategory.DIAGNOSTIC,
state_class=SensorStateClass.MEASUREMENT,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UNIT_COST,
icon="mdi:currency-ils",
reset_policy=ResetPolicy.MONTHLY,
),
IntegrationNumberEntityDescription(
key=EntityKeys.SEWAGE_COST,
Expand Down
6 changes: 6 additions & 0 deletions custom_components/citymind_water_meter/common/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class EntityType(StrEnum):
ACCOUNT = "account"


class ResetPolicy(Enum):
NONE = 0
DAILY = 1
MONTHLY = 2


class EntityKeys(StrEnum):
CONSUMPTION_FORECAST = "consumption_forecast"
LAST_READ = "last_read"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class BaseProcessor:
_last_name: str | None = None
_today_iso: str | None = None
_yesterday_iso: str | None = None
_current_month_iso: str | None = None
_config_manager: ConfigManager | None = None
_config_data: ConfigData | None = None
_unique_messages: list[str] | None = None
Expand All @@ -35,16 +36,18 @@ def __init__(self, config_manager: ConfigManager):
self._account_number = None
self._first_name = None
self._last_name = None
self._today_iso = None
self._yesterday_iso = None
self.processor_type = None

self._unique_messages = []

def update(self, api_data: dict, today_iso: str, yesterday_iso: str):
def update(self, api_data: dict):
self._api_data = api_data
self._today_iso = today_iso
self._yesterday_iso = yesterday_iso

analytic_periods = self._config_manager.analytic_periods

self._today_iso = analytic_periods.today_iso
self._yesterday_iso = analytic_periods.yesterday_iso
self._current_month_iso = analytic_periods.current_month_iso

self._process_api_data()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,14 @@ def _load_meter(
last_read_value = last_read_details.get(meter_id, 0)
last_read = self._format_number(last_read_value, 3)

date_parts = self._today_iso.split("-")
month_parts = date_parts[:2]
current_month = "-".join(month_parts)

yesterday_consumption = self._get_consumption(
daily_consumption_section, meter_id, self._yesterday_iso
)
today_consumption = self._get_consumption(
daily_consumption_section, meter_id, self._today_iso
)
monthly_consumption = self._get_consumption(
monthly_consumption_section, meter_id, current_month
monthly_consumption_section, meter_id, self._current_month_iso
)

consumption_forecast_data = consumption_forecast_section.get(str(meter_id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
STORAGE_DATA_METERS,
)
from ..common.entity_descriptions import IntegrationEntityDescription
from ..models.analytics_periods import AnalyticPeriodsData
from ..models.config_data import ConfigData

_LOGGER = logging.getLogger(__name__)
Expand All @@ -47,6 +48,8 @@ class ConfigManager:
_is_set_up_mode: bool
_is_initialized: bool

analytic_periods: AnalyticPeriodsData

def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None = None):
self._hass = hass
self._entry = entry
Expand All @@ -65,6 +68,8 @@ def __init__(self, hass: HomeAssistant | None, entry: ConfigEntry | None = None)
self._is_set_up_mode = entry is None
self._is_initialized = False

self.analytic_periods = AnalyticPeriodsData()

if hass is not None:
self._store = Store(
hass, STORAGE_VERSION, CONFIGURATION_FILE, encoder=JSONEncoder
Expand Down
39 changes: 37 additions & 2 deletions custom_components/citymind_water_meter/managers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ def __init__(self, hass, config_manager: ConfigManager):
entry.async_on_unload(async_dispatcher_connect(hass, signal, handler))

config_data = config_manager.config_data
analytic_periods = config_manager.analytic_periods
entry_id = config_manager.entry_id

self._api = RestAPI(self.hass, config_data, entry_id)
self._api = RestAPI(self.hass, config_data, analytic_periods, entry_id)

self._config_manager = config_manager

Expand Down Expand Up @@ -176,6 +177,8 @@ async def _on_api_status_changed(self, entry_id: str, status: ConnectivityStatus
return

if status == ConnectivityStatus.Connected:
self.config_manager.analytic_periods.update()

await self._api.update()

elif status in [ConnectivityStatus.Failed]:
Expand Down Expand Up @@ -222,7 +225,7 @@ async def _on_data_changed(self, entry_id: str):
if api_connected:
for processor_type in self._processors:
processor = self._processors[processor_type]
processor.update(self._api.data, self._api.today, self._api.yesterday)
processor.update(self._api.data)

account = self._account_processor.get()

Expand Down Expand Up @@ -252,6 +255,8 @@ async def _async_update_data(self):
now = datetime.now().timestamp()

if now - self._last_update >= self.update_interval.total_seconds():
self.config_manager.analytic_periods.update()

await self._api.update()

self._last_update = now
Expand All @@ -277,8 +282,11 @@ def _build_data_mapping(self):
EntityKeys.HIGH_RATE_CONSUMPTION: self._get_high_rate_consumption_data,
EntityKeys.LOW_RATE_CONSUMPTION: self._get_low_rate_consumption_data,
EntityKeys.LOW_RATE_COST: self._get_low_rate_cost_data,
EntityKeys.LOW_RATE_TOTAL_COST: self._get_low_rate_total_cost_data,
EntityKeys.HIGH_RATE_COST: self._get_high_rate_cost_data,
EntityKeys.HIGH_RATE_TOTAL_COST: self._get_high_rate_total_cost_data,
EntityKeys.SEWAGE_COST: self._get_sewage_cost_data,
EntityKeys.SEWAGE_TOTAL_COST: self._get_sewage_total_cost_data,
EntityKeys.LOW_RATE_CONSUMPTION_THRESHOLD: self._get_low_rate_consumption_threshold_data,
EntityKeys.ALERTS: self._get_alerts_data,
EntityKeys.ALERT_EXCEEDED_THRESHOLD_SMS: self._get_alert_setting_data,
Expand Down Expand Up @@ -448,6 +456,15 @@ def _get_low_rate_cost_data(

return result

def _get_low_rate_total_cost_data(
self, _entity_description, meter_id: str
) -> dict | None:
data = self._meter_processor.get_data(meter_id)

result = {ATTR_STATE: data.low_rate_cost * data.low_rate_monthly_consumption}

return result

def _get_high_rate_cost_data(
self, _entity_description, meter_id: str
) -> dict | None:
Expand All @@ -462,6 +479,15 @@ def _get_high_rate_cost_data(

return result

def _get_high_rate_total_cost_data(
self, _entity_description, meter_id: str
) -> dict | None:
data = self._meter_processor.get_data(meter_id)

result = {ATTR_STATE: data.high_rate_cost * data.high_rate_monthly_consumption}

return result

def _get_sewage_cost_data(self, _entity_description, meter_id: str) -> dict | None:
data = self._meter_processor.get_data(meter_id)

Expand All @@ -474,6 +500,15 @@ def _get_sewage_cost_data(self, _entity_description, meter_id: str) -> dict | No

return result

def _get_sewage_total_cost_data(
self, _entity_description, meter_id: str
) -> dict | None:
data = self._meter_processor.get_data(meter_id)

result = {ATTR_STATE: data.sewage_cost * data.monthly_consumption}

return result

def _get_low_rate_consumption_threshold_data(
self, _entity_description, meter_id: str
) -> dict | None:
Expand Down
Loading

0 comments on commit f602af3

Please sign in to comment.