Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Allow sensors to be used in the HA Energy Dashboard #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions pyhydroquebec/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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'),
Expand Down
15 changes: 11 additions & 4 deletions pyhydroquebec/mqtt_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
Expand Down