Skip to content

Commit 6a11e9b

Browse files
authored
Merge pull request #9 from bertouttier/update_new_mqtt_schema
update to new MQTT schema
2 parents c4b872c + 96a2998 commit 6a11e9b

File tree

6 files changed

+112
-76
lines changed

6 files changed

+112
-76
lines changed

custom_components/flukso/__init__.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
from homeassistant.config_entries import ConfigEntry
55
from homeassistant.core import HomeAssistant
66
from homeassistant.helpers.typing import ConfigType
7+
from homeassistant.const import Platform
78

8-
from .const import CONF_DEVICE_HASH, DOMAIN, PLATFORMS
9+
from .const import CONF_DEVICE_HASH, DOMAIN
910
from .discovery import async_discover_device
1011

12+
PLATFORMS: list[Platform] = [Platform.SENSOR, Platform.BINARY_SENSOR]
13+
1114

1215
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
1316
"""Set up Flukso integration."""
@@ -21,12 +24,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2124
if entry.data[CONF_DEVICE_HASH] == data[CONF_DEVICE_HASH]:
2225
return False
2326

24-
hass.data[DOMAIN][entry.entry_id] = {CONF_DEVICE_HASH: entry.data[CONF_DEVICE_HASH]}
27+
hass.data[DOMAIN][entry.entry_id] = {
28+
CONF_DEVICE_HASH: entry.data[CONF_DEVICE_HASH]
29+
}
2530

2631
await async_discover_device(hass, entry)
27-
28-
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
29-
32+
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
3033
return True
3134

3235

@@ -38,7 +41,9 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3841

3942
device_registry = await hass.helpers.device_registry.async_get_registry()
4043
device = device_registry.async_get_device(
41-
identifiers={(DOMAIN, entry.data[CONF_DEVICE_HASH])}
44+
identifiers={
45+
(DOMAIN, entry.data[CONF_DEVICE_HASH])
46+
}
4247
)
4348

4449
if device is not None:
Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
"""Flukso binary sensor."""
2-
from homeassistant.components.mqtt.binary_sensor import MqttBinarySensor
2+
import functools
3+
4+
from homeassistant.components import binary_sensor
5+
from homeassistant.components.mqtt.binary_sensor import (DISCOVERY_SCHEMA,
6+
MqttBinarySensor)
7+
from homeassistant.components.mqtt.mixins import async_setup_entry_helper
8+
from homeassistant.const import Platform
39

410
from .const import DOMAIN
511
from .discovery import get_entities_for_platform
612

713

8-
async def async_setup_entry(hass, entry, async_add_entities):
9-
"""Add a Flukso binary sensor."""
10-
configs = get_entities_for_platform(
11-
"binary_sensor", hass.data[DOMAIN][entry.entry_id]
14+
async def async_setup_entry(hass, config_entry, async_add_entities):
15+
"""Set up MQTT binary sensor through configuration.yaml and dynamically through MQTT discovery."""
16+
setup = functools.partial(
17+
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
1218
)
19+
await async_setup_entry_helper(hass, binary_sensor.DOMAIN, setup, DISCOVERY_SCHEMA)
20+
1321

22+
async def _async_setup_entity(hass, async_add_entities, config, config_entry=None, discovery_data=None):
23+
"""Set up MQTT binary sensor."""
24+
configs = get_entities_for_platform(
25+
Platform.BINARY_SENSOR,
26+
hass.data[DOMAIN][config_entry.entry_id]
27+
)
1428
async_add_entities(
15-
[MqttBinarySensor(hass, config, entry, None) for config in configs]
29+
[MqttBinarySensor(hass, c, config_entry, discovery_data) for c in configs]
1630
)

custom_components/flukso/config_flow.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from typing import Any
66

77
import voluptuous as vol
8-
98
from homeassistant import config_entries
109
from homeassistant.data_entry_flow import FlowResult
1110

custom_components/flukso/const.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,4 @@
66
CONF_DEVICE_SERIAL = "device_serial"
77
CONF_DEVICE_FIRMWARE = "device_firmware"
88

9-
DEFAULT_TIMEOUT = 10
10-
11-
PLATFORMS = [
12-
"binary_sensor",
13-
"sensor",
14-
]
9+
DEFAULT_TIMEOUT = 10

custom_components/flukso/discovery.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,36 @@
44
import logging
55
import re
66

7-
from homeassistant.components.mqtt import CONF_QOS, CONF_STATE_TOPIC, subscription
8-
from homeassistant.components.mqtt.binary_sensor import (
9-
CONF_OFF_DELAY,
10-
PLATFORM_SCHEMA as MQTT_BINARY_SENSOR_PLATFORM_SCHEMA,
11-
)
12-
from homeassistant.components.mqtt.mixins import (
13-
CONF_CONNECTIONS,
14-
CONF_ENABLED_BY_DEFAULT,
15-
CONF_IDENTIFIERS,
16-
CONF_MANUFACTURER,
17-
CONF_SW_VERSION,
18-
)
19-
from homeassistant.components.mqtt.sensor import (
20-
CONF_STATE_CLASS,
21-
PLATFORM_SCHEMA as MQTT_SENSOR_PLATFORM_SCHEMA,
22-
)
237
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
24-
from homeassistant.components.sensor import (
25-
SensorStateClass,
26-
SensorDeviceClass
27-
)
28-
from homeassistant.const import (
29-
CONF_DEVICE,
30-
CONF_DEVICE_CLASS,
31-
CONF_FORCE_UPDATE,
32-
CONF_ICON,
33-
CONF_NAME,
34-
CONF_PLATFORM,
35-
CONF_UNIQUE_ID,
36-
CONF_UNIT_OF_MEASUREMENT,
37-
CONF_VALUE_TEMPLATE,
38-
ELECTRIC_CURRENT_AMPERE,
39-
ELECTRIC_POTENTIAL_VOLT,
40-
ENERGY_WATT_HOUR,
41-
LIGHT_LUX,
42-
PERCENTAGE,
43-
POWER_WATT,
44-
PRESSURE_HPA,
45-
TEMP_CELSIUS,
46-
VOLUME_CUBIC_METERS,
47-
VOLUME_LITERS,
48-
)
8+
from homeassistant.components.mqtt import (CONF_QOS, CONF_STATE_TOPIC,
9+
subscription)
10+
from homeassistant.components.mqtt.binary_sensor import CONF_OFF_DELAY
11+
from homeassistant.components.mqtt.binary_sensor import \
12+
PLATFORM_SCHEMA_MODERN as MQTT_BINARY_SENSOR_PLATFORM_SCHEMA
13+
from homeassistant.components.mqtt.mixins import (CONF_CONNECTIONS,
14+
CONF_ENABLED_BY_DEFAULT,
15+
CONF_IDENTIFIERS,
16+
CONF_MANUFACTURER,
17+
CONF_OBJECT_ID,
18+
CONF_SW_VERSION)
19+
from homeassistant.components.mqtt.sensor import CONF_STATE_CLASS
20+
from homeassistant.components.mqtt.sensor import \
21+
PLATFORM_SCHEMA_MODERN as MQTT_SENSOR_PLATFORM_SCHEMA
22+
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
23+
from homeassistant.const import (CONF_DEVICE, CONF_DEVICE_CLASS,
24+
CONF_ENTITY_CATEGORY, CONF_FORCE_UPDATE,
25+
CONF_ICON, CONF_NAME, CONF_UNIQUE_ID,
26+
CONF_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
27+
ELECTRIC_CURRENT_AMPERE,
28+
ELECTRIC_POTENTIAL_VOLT, ENERGY_WATT_HOUR,
29+
LIGHT_LUX, PERCENTAGE, POWER_WATT,
30+
PRESSURE_HPA, TEMP_CELSIUS,
31+
VOLUME_CUBIC_METERS, VOLUME_LITERS, Platform)
4932
from homeassistant.core import callback
33+
from homeassistant.helpers.entity import EntityCategory
5034

51-
from .const import (
52-
CONF_DEVICE_FIRMWARE,
53-
CONF_DEVICE_HASH,
54-
CONF_DEVICE_SERIAL,
55-
DEFAULT_TIMEOUT,
56-
DOMAIN,
57-
)
35+
from .const import (CONF_DEVICE_FIRMWARE, CONF_DEVICE_HASH, CONF_DEVICE_SERIAL,
36+
DEFAULT_TIMEOUT, DOMAIN)
5837

5938
_LOGGER = logging.getLogger(__name__)
6039

@@ -227,6 +206,31 @@ def _get_sensor_detail(sensor, detail_map):
227206

228207

229208
def _get_sensor_name(sensor, entry_data):
209+
"""Generate a name based on the kube and flx config, and the data type and sub type."""
210+
name = "unknown"
211+
if "class" in sensor and sensor["class"] == "kube":
212+
name = "unknown kube"
213+
if (
214+
"kube" in entry_data
215+
and "name" in entry_data["kube"][str(sensor["kid"])]
216+
and entry_data["kube"][str(sensor["kid"])]["name"]
217+
):
218+
name = entry_data["kube"][str(sensor["kid"])]["name"]
219+
else:
220+
"unknown sensor"
221+
if "port" in sensor:
222+
if "function" in sensor:
223+
name = sensor["function"]
224+
elif (
225+
"flx" in entry_data
226+
and "name" in entry_data["flx"][str(sensor["port"][0])]
227+
and entry_data["flx"][str(sensor["port"][0])]["name"]
228+
):
229+
name = entry_data["flx"][str(sensor["port"][0])]["name"]
230+
return name
231+
232+
233+
def _get_sensor_object_id(sensor, entry_data):
230234
"""Generate a name based on the kube and flx config, and the data type and sub type."""
231235
name = "unknown"
232236
if "class" in sensor and sensor["class"] == "kube":
@@ -283,9 +287,10 @@ def _get_binary_sensor_entities(entry_data, device_info):
283287

284288
sensorconfig = {}
285289
sensorconfig[CONF_NAME] = _get_sensor_name(sensor, entry_data)
290+
sensorconfig[CONF_OBJECT_ID] = _get_sensor_object_id(sensor, entry_data)
286291
sensorconfig[CONF_DEVICE] = device_info
292+
sensorconfig[CONF_ENTITY_CATEGORY] = EntityCategory.DIAGNOSTIC
287293
sensorconfig[CONF_ENABLED_BY_DEFAULT] = True
288-
sensorconfig[CONF_PLATFORM] = "mqtt"
289294
sensorconfig[CONF_STATE_TOPIC] = f'/sensor/{sensor["id"]}/{sensor["data_type"]}'
290295
sensorconfig[CONF_QOS] = 0
291296
sensorconfig[CONF_FORCE_UPDATE] = False
@@ -342,9 +347,10 @@ def _get_binary_sensor_entities(entry_data, device_info):
342347
def _get_sensor_config(sensor, entry_data, device_info):
343348
sensorconfig = {}
344349
sensorconfig[CONF_NAME] = _get_sensor_name(sensor, entry_data)
350+
sensorconfig[CONF_OBJECT_ID] = _get_sensor_object_id(sensor, entry_data)
345351
sensorconfig[CONF_DEVICE] = device_info
352+
sensorconfig[CONF_ENTITY_CATEGORY] = EntityCategory.DIAGNOSTIC
346353
sensorconfig[CONF_ENABLED_BY_DEFAULT] = True
347-
sensorconfig[CONF_PLATFORM] = "mqtt"
348354
sensorconfig[CONF_STATE_TOPIC] = f'/sensor/{sensor["id"]}/{sensor["data_type"]}'
349355
sensorconfig[CONF_STATE_CLASS] = _get_sensor_detail(sensor, STATE_CLASS_MAP)
350356
sensorconfig[CONF_QOS] = 0
@@ -433,9 +439,9 @@ def get_entities_for_platform(platform, entry_data):
433439
"""Generate configuration for the given platform."""
434440
entities = []
435441
device_info = _get_device_info(entry_data)
436-
if platform == "binary_sensor":
442+
if platform == Platform.BINARY_SENSOR:
437443
entities.extend(_get_binary_sensor_entities(entry_data, device_info))
438-
elif platform == "sensor":
444+
elif platform == Platform.SENSOR:
439445
entities.extend(_get_sensor_entities(entry_data, device_info))
440446
return entities
441447

custom_components/flukso/sensor.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
"""Flukso sensor."""
2-
from homeassistant.components.mqtt.sensor import MqttSensor
2+
import functools
3+
4+
from homeassistant.components import sensor
5+
from homeassistant.components.mqtt.mixins import async_setup_entry_helper
6+
from homeassistant.components.mqtt.sensor import DISCOVERY_SCHEMA, MqttSensor
7+
from homeassistant.const import Platform
38

49
from .const import DOMAIN
510
from .discovery import get_entities_for_platform
611

712

8-
async def async_setup_entry(hass, entry, async_add_entities):
9-
"""Add a Flukso sensor."""
10-
configs = get_entities_for_platform("sensor", hass.data[DOMAIN][entry.entry_id])
13+
async def async_setup_entry(hass, config_entry, async_add_entities):
14+
"""Set up MQTT sensor through configuration.yaml and dynamically through MQTT discovery."""
15+
setup = functools.partial(
16+
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
17+
)
18+
await async_setup_entry_helper(hass, sensor.DOMAIN, setup, DISCOVERY_SCHEMA)
1119

12-
async_add_entities([MqttSensor(hass, config, entry, None) for config in configs])
20+
21+
async def _async_setup_entity(hass, async_add_entities, config, config_entry=None, discovery_data=None):
22+
"""Add a Flukso sensor."""
23+
configs = get_entities_for_platform(
24+
Platform.SENSOR,
25+
hass.data[DOMAIN][config_entry.entry_id]
26+
)
27+
async_add_entities(
28+
[MqttSensor(hass, c, config_entry, discovery_data) for c in configs]
29+
)

0 commit comments

Comments
 (0)