diff --git a/pyhydroquebec/consts.py b/pyhydroquebec/consts.py index 4ce13a0..0082bdb 100644 --- a/pyhydroquebec/consts.py +++ b/pyhydroquebec/consts.py @@ -47,45 +47,55 @@ "resourceObtenirDonneesMeteoHoraires".format(HOST_SPRING)) CURRENT_MAP = {'period_total_bill': {'raw_name': 'montantFacturePeriode', - 'unit': '$', + 'unit': 'CAD', 'icon': 'mdi:currency-usd', - 'device_class': None}, + 'device_class': 'monetary', + 'state_class': 'total_increasing'}, 'period_projection': {'raw_name': 'montantProjetePeriode', - 'unit': '$', + 'unit': 'CAD', 'icon': 'mdi:currency-usd', - 'device_class': None}, + 'device_class': 'monetary', + 'state_class': None}, 'period_length': {'raw_name': 'nbJourLecturePeriode', 'unit': 'days', 'icon': 'mdi:calendar-range', - 'device_class': None}, + 'device_class': None, + 'state_class': None}, 'period_total_days': {'raw_name': 'nbJourPrevuPeriode', 'unit': 'days', 'icon': 'mdi:calendar-range', - 'device_class': None}, + 'device_class': None, + 'state_class': None}, 'period_mean_daily_bill': {'raw_name': 'moyenneDollarsJourPeriode', - 'unit': '$', + 'unit': 'CAD', 'icon': 'mdi:currency-usd', - 'device_class': None}, + 'device_class': 'monetary', + 'state_class': None}, 'period_mean_daily_consumption': {'raw_name': 'moyenneKwhJourPeriode', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': 'energy', + 'state_class': None}, 'period_total_consumption': {'raw_name': 'consoTotalPeriode', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': 'energy', + 'state_class': 'total_increasing'}, 'period_lower_price_consumption': {'raw_name': 'consoRegPeriode', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': 'energy', + 'state_class': None}, 'period_higher_price_consumption': {'raw_name': 'consoHautPeriode', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': 'energy', + 'state_class': None}, 'period_average_temperature': {'raw_name': 'tempMoyennePeriode', 'unit': '°C', - 'icon': None, - 'device_class': 'temperature'} + 'icon': 'mdi:thermometer', + 'device_class': 'temperature', + 'state_class': None} } MONTHLY_MAP = (('conso_code', 'codeConsoMois'), @@ -98,21 +108,25 @@ ) DAILY_MAP = {'total_consumption': {'raw_name': 'consoTotalQuot', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': 'energy', + 'state_class': None}, 'lower_price_consumption': {'raw_name': 'consoRegQuot', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': None, + 'state_class': None}, 'higher_price_consumption': {'raw_name': 'consoHautQuot', - 'unit': 'Kwh', + 'unit': 'kWh', 'icon': 'mdi:flash', - 'device_class': None}, + 'device_class': None, + 'state_class': None}, 'average_temperature': {'raw_name': 'tempMoyenneQuot', 'unit': '°C', - 'icon': None, - 'device_class': 'temperature'} + 'icon': 'mdi:thermometer', + 'device_class': 'temperature', + 'state_class': None} } ANNUAL_MAP = (('annual_mean_daily_consumption', 'moyenneKwhJourAnnee'), ('annual_total_consumption', 'consoTotalAnnee'), diff --git a/pyhydroquebec/mqtt_daemon.py b/pyhydroquebec/mqtt_daemon.py index 79fc51b..93d8f68 100644 --- a/pyhydroquebec/mqtt_daemon.py +++ b/pyhydroquebec/mqtt_daemon.py @@ -49,7 +49,7 @@ async def _init_main_loop(self): """Init before starting main loop.""" def _publish_sensor(self, sensor_type, contract_id, - unit=None, device_class=None, icon=None): + unit=None, device_class=None, icon=None, state_class=None): """Publish a Home-Assistant MQTT sensor.""" mac_addr = get_mac() @@ -74,6 +74,8 @@ def _publish_sensor(self, sensor_type, contract_id, if device_class: sensor_config["device_class"] = device_class + if state_class: + sensor_config["state_class"] = state_class if unit: sensor_config["unit_of_measurement"] = unit if icon: @@ -121,11 +123,12 @@ async def _main_loop(self): # Balance # Publish sensor balance_topic = self._publish_sensor('balance', customer.account_id, - unit="$", device_class=None, + unit="CAD", device_class='monetary', icon="mdi:currency-usd") # Send sensor data self.mqtt_client.publish( topic=balance_topic, + retain=True, payload=customer.balance) # Current period @@ -135,10 +138,12 @@ async def _main_loop(self): customer.contract_id, unit=data['unit'], icon=data['icon'], - device_class=data['device_class']) + device_class=data['device_class'], + state_class=data['state_class']) # Send sensor data self.mqtt_client.publish( topic=sensor_topic, + retain=True, payload=customer.current_period[data_name]) # Yesterday data @@ -148,10 +153,12 @@ async def _main_loop(self): customer.contract_id, unit=data['unit'], icon=data['icon'], - device_class=data['device_class']) + device_class=data['device_class'], + state_class=data['state_class']) # Send sensor data self.mqtt_client.publish( topic=sensor_topic, + retain=True, payload=customer.current_daily_data[yesterday_str][data_name]) await client.close_session()