Skip to content

Commit

Permalink
Clean up and add STATE sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
megakid committed Jun 14, 2022
1 parent 224076b commit 29f823a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 66 deletions.
39 changes: 0 additions & 39 deletions custom_components/hildebrand_glow_ihd_mqtt/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,3 @@ async def async_step_user(self, user_input=None):
data={
CONF_DEVICE_ID: unique_id,
})

# @staticmethod
# @callback
# def async_get_options_flow(config_entry):
# return GardenaSmartSystemOptionsFlowHandler(config_entry)


# class GardenaSmartSystemOptionsFlowHandler(config_entries.OptionsFlow):
# def __init__(self, config_entry):
# """Initialize Gardena Smart Sytem options flow."""
# self.config_entry = config_entry

# async def async_step_init(self, user_input=None):
# """Manage the options."""
# return await self.async_step_user()

# async def async_step_user(self, user_input=None):
# """Handle a flow initialized by the user."""
# errors = {}
# if user_input is not None:
# # TODO: Validate options (min, max values)
# return self.async_create_entry(title="", data=user_input)

# fields = OrderedDict()
# fields[vol.Optional(
# CONF_MOWER_DURATION,
# default=self.config_entry.options.get(
# CONF_MOWER_DURATION, DEFAULT_MOWER_DURATION))] = cv.positive_int
# fields[vol.Optional(
# CONF_SMART_IRRIGATION_DURATION,
# default=self.config_entry.options.get(
# CONF_SMART_IRRIGATION_DURATION, DEFAULT_SMART_IRRIGATION_DURATION))] = cv.positive_int
# fields[vol.Optional(
# CONF_SMART_WATERING_DURATION,
# default=self.config_entry.options.get(
# CONF_SMART_WATERING_DURATION, DEFAULT_SMART_WATERING_DURATION))] = cv.positive_int

# return self.async_show_form(step_id="user", data_schema=vol.Schema(fields), errors=errors)

78 changes: 51 additions & 27 deletions custom_components/hildebrand_glow_ihd_mqtt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import json
import re
import logging
from turtle import update
from typing import Iterable

from homeassistant.components import mqtt
Expand All @@ -14,14 +13,16 @@
SensorDeviceClass,
SensorStateClass,
)
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from .const import DOMAIN
from homeassistant.const import (
CONF_DEVICE_ID,
ATTR_DEVICE_ID,

ENERGY_KILO_WATT_HOUR,
POWER_KILO_WATT
POWER_KILO_WATT,
SIGNAL_STRENGTH_DECIBELS,
PERCENTAGE,
)
from homeassistant.core import callback
from homeassistant.helpers.entity import DeviceInfo
Expand All @@ -39,17 +40,36 @@
"device_class": None,
"unit_of_measurement": None,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:information-outline",
"func": lambda js: js["software"],

},
{
"name": "Smart Meter IHD Hardware",
"device_class": "connectivity",
"unit_of_measurement": "",
"device_class": None,
"unit_of_measurement": None,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:information-outline",
"func": lambda js: js["software"]
"func": lambda js: js["hardware"],
},
{
"name": "Smart Meter IHD HAN RSSI",
"device_class": SensorDeviceClass.SIGNAL_STRENGTH,
"unit_of_measurement": SIGNAL_STRENGTH_DECIBELS,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:wifi-strength-outline",
"func": lambda js: js["han"]["rssi"]
},
{
"name": "Smart Meter IHD HAN LQI",
"device_class": None,
"unit_of_measurement": PERCENTAGE,
"state_class": SensorStateClass.MEASUREMENT,
"entity_category": EntityCategory.DIAGNOSTIC,
"icon": "mdi:wifi-strength-outline",
"func": lambda js: js["han"]["lqi"]
}
]

Expand Down Expand Up @@ -200,13 +220,13 @@
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the Smart Meter sensors."""

# the config is defaulted to + which happens to mean we will subscribe to all devices (note, THERE MUST ONLY BE ONE!)
# the config is defaulted to + which happens to mean we will subscribe to all devices
device_mac = hass.data[DOMAIN][config_entry.entry_id][CONF_DEVICE_ID]

deviceUpdateGroups = {}

@callback
async def mqtt_message_received(message: ReceiveMessage) -> None:
async def mqtt_message_received(message: ReceiveMessage):
"""Handle received MQTT message."""
topic = message.topic
payload = message.payload
Expand All @@ -218,7 +238,7 @@ async def mqtt_message_received(message: ReceiveMessage) -> None:
for updateGroup in updateGroups:
updateGroup.process_update(message)

data_topic = f"glow/#"
data_topic = "glow/#"

await mqtt.async_subscribe(
hass, data_topic, mqtt_message_received, 1
Expand All @@ -227,19 +247,21 @@ async def mqtt_message_received(message: ReceiveMessage) -> None:


async def async_get_device_groups(deviceUpdateGroups, async_add_entities, device_id):
#add to update groups if not already there
if device_id not in deviceUpdateGroups:
_LOGGER.debug("New device found: %s", device_id)
groups = [
HildebrandGlowMqttSensorUpdateGroup(device_id, async_add_entities, "electricitymeter", ELECTRICITY_SENSORS),
HildebrandGlowMqttSensorUpdateGroup(device_id, async_add_entities, "gasmeter", GAS_SENSORS)
]
await async_add_entities(
[sensorEntity for updateGroup in groups for sensorEntity in updateGroup.all_sensors]
)
deviceUpdateGroups[device_id] = groups
#add to update groups if not already there
if device_id not in deviceUpdateGroups:
_LOGGER.debug("New device found: %s", device_id)
groups = [
HildebrandGlowMqttSensorUpdateGroup(device_id, "STATE", STATE_SENSORS),
HildebrandGlowMqttSensorUpdateGroup(device_id, "electricitymeter", ELECTRICITY_SENSORS),
HildebrandGlowMqttSensorUpdateGroup(device_id, "gasmeter", GAS_SENSORS)
]
async_add_entities(
[sensorEntity for updateGroup in groups for sensorEntity in updateGroup.all_sensors],
#True
)
deviceUpdateGroups[device_id] = groups

return deviceUpdateGroups[device_id]
return deviceUpdateGroups[device_id]


class HildebrandGlowMqttSensorUpdateGroup:
Expand Down Expand Up @@ -268,7 +290,7 @@ def all_sensors(self) -> Iterable[HildebrandGlowMqttSensor]:
class HildebrandGlowMqttSensor(SensorEntity):
"""Representation of a room sensor that is updated via MQTT."""

def __init__(self, device_id, name, icon, device_class, unit_of_measurement, state_class, func, ignore_zero_values = False) -> None:
def __init__(self, device_id, name, icon, device_class, unit_of_measurement, state_class, func, entity_category = EntityCategory.CONFIG, ignore_zero_values = False) -> None:

This comment has been minimized.

Copy link
@ginkage

ginkage Jun 15, 2022

But why?..
Before this, all entities were showing up automatically, straight away, and now I need to add them manually.

This comment has been minimized.

Copy link
@ginkage

ginkage Jun 15, 2022

OK, I see there's only "Configuration" and "Diagnostic" available... Oh well.

This comment has been minimized.

Copy link
@megakid

megakid Jun 15, 2022

Author Owner

Sorry, I dont understand - what are you having trouble with? The entity_category simply categorises the entities - it shouldn't affect when or how they show up

"""Initialize the sensor."""
self._device_id = device_id
self._ignore_zero_values = ignore_zero_values
Expand All @@ -278,12 +300,15 @@ def __init__(self, device_id, name, icon, device_class, unit_of_measurement, sta
self._attr_device_class = device_class
self._attr_native_unit_of_measurement = unit_of_measurement
self._attr_state_class = state_class
self._attr_entity_category = entity_category
self._attr_should_poll = False

self._func = func
self._attr_device_info = DeviceInfo(
connections={("mac", device_id)},
manufacturer="Hildebrand Technology Limited",
model="Glow Smart Meter IHD",
name=device_id,
name=f"Glow Smart Meter {device_id}",
)
self._attr_native_value = None

Expand All @@ -294,11 +319,10 @@ def process_update(self, mqtt_data) -> None:
_LOGGER.debug("Ignored new value of %s on %s.", new_value, self._attr_unique_id)
return
self._attr_native_value = new_value
self.async_write_ha_state()
if (self.hass is not None): # this is a hack to get around the fact that the entity is not yet initialized at first
self.async_schedule_update_ha_state()

@property
def extra_state_attributes(self):
"""Return the state attributes."""
if (self._device_id == "+"):
return {}
return {ATTR_DEVICE_ID: self._device_id}

0 comments on commit 29f823a

Please sign in to comment.