Use platform tag to register components on hue SensorManager (#32732)
* Use platform tag to register components on hue SensorManager instead of a boolean flag to decide between sensor and binary sensor, so it could be used externally (or to get ready for inclusion of other comps) * Make new item discovery platform agnostic for SensorManager
This commit is contained in:
parent
11a25157c1
commit
94b6ab2862
3 changed files with 14 additions and 17 deletions
|
@ -17,7 +17,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Defer binary sensor setup to the shared sensor module."""
|
"""Defer binary sensor setup to the shared sensor module."""
|
||||||
await hass.data[HUE_DOMAIN][
|
await hass.data[HUE_DOMAIN][
|
||||||
config_entry.entry_id
|
config_entry.entry_id
|
||||||
].sensor_manager.async_register_component(True, async_add_entities)
|
].sensor_manager.async_register_component("binary_sensor", async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
class HuePresence(GenericZLLSensor, BinarySensorDevice):
|
class HuePresence(GenericZLLSensor, BinarySensorDevice):
|
||||||
|
@ -44,7 +44,7 @@ class HuePresence(GenericZLLSensor, BinarySensorDevice):
|
||||||
SENSOR_CONFIG_MAP.update(
|
SENSOR_CONFIG_MAP.update(
|
||||||
{
|
{
|
||||||
TYPE_ZLL_PRESENCE: {
|
TYPE_ZLL_PRESENCE: {
|
||||||
"binary": True,
|
"platform": "binary_sensor",
|
||||||
"name_format": PRESENCE_NAME_FORMAT,
|
"name_format": PRESENCE_NAME_FORMAT,
|
||||||
"class": HuePresence,
|
"class": HuePresence,
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
"""Defer sensor setup to the shared sensor module."""
|
"""Defer sensor setup to the shared sensor module."""
|
||||||
await hass.data[HUE_DOMAIN][
|
await hass.data[HUE_DOMAIN][
|
||||||
config_entry.entry_id
|
config_entry.entry_id
|
||||||
].sensor_manager.async_register_component(False, async_add_entities)
|
].sensor_manager.async_register_component("sensor", async_add_entities)
|
||||||
|
|
||||||
|
|
||||||
class GenericHueGaugeSensorEntity(GenericZLLSensor, Entity):
|
class GenericHueGaugeSensorEntity(GenericZLLSensor, Entity):
|
||||||
|
@ -82,12 +82,12 @@ class HueTemperature(GenericHueGaugeSensorEntity):
|
||||||
SENSOR_CONFIG_MAP.update(
|
SENSOR_CONFIG_MAP.update(
|
||||||
{
|
{
|
||||||
TYPE_ZLL_LIGHTLEVEL: {
|
TYPE_ZLL_LIGHTLEVEL: {
|
||||||
"binary": False,
|
"platform": "sensor",
|
||||||
"name_format": LIGHT_LEVEL_NAME_FORMAT,
|
"name_format": LIGHT_LEVEL_NAME_FORMAT,
|
||||||
"class": HueLightLevel,
|
"class": HueLightLevel,
|
||||||
},
|
},
|
||||||
TYPE_ZLL_TEMPERATURE: {
|
TYPE_ZLL_TEMPERATURE: {
|
||||||
"binary": False,
|
"platform": "sensor",
|
||||||
"name_format": TEMPERATURE_NAME_FORMAT,
|
"name_format": TEMPERATURE_NAME_FORMAT,
|
||||||
"class": HueTemperature,
|
"class": HueTemperature,
|
||||||
},
|
},
|
||||||
|
|
|
@ -62,9 +62,9 @@ class SensorManager:
|
||||||
except AiohueException as err:
|
except AiohueException as err:
|
||||||
raise UpdateFailed(f"Hue error: {err}")
|
raise UpdateFailed(f"Hue error: {err}")
|
||||||
|
|
||||||
async def async_register_component(self, binary, async_add_entities):
|
async def async_register_component(self, platform, async_add_entities):
|
||||||
"""Register async_add_entities methods for components."""
|
"""Register async_add_entities methods for components."""
|
||||||
self._component_add_entities[binary] = async_add_entities
|
self._component_add_entities[platform] = async_add_entities
|
||||||
|
|
||||||
if len(self._component_add_entities) < 2:
|
if len(self._component_add_entities) < 2:
|
||||||
return
|
return
|
||||||
|
@ -84,8 +84,7 @@ class SensorManager:
|
||||||
if len(self._component_add_entities) < 2:
|
if len(self._component_add_entities) < 2:
|
||||||
return
|
return
|
||||||
|
|
||||||
new_sensors = []
|
to_add = {}
|
||||||
new_binary_sensors = []
|
|
||||||
primary_sensor_devices = {}
|
primary_sensor_devices = {}
|
||||||
current = self.current
|
current = self.current
|
||||||
|
|
||||||
|
@ -129,10 +128,10 @@ class SensorManager:
|
||||||
current[api[item_id].uniqueid] = sensor_config["class"](
|
current[api[item_id].uniqueid] = sensor_config["class"](
|
||||||
api[item_id], name, self.bridge, primary_sensor=primary_sensor
|
api[item_id], name, self.bridge, primary_sensor=primary_sensor
|
||||||
)
|
)
|
||||||
if sensor_config["binary"]:
|
|
||||||
new_binary_sensors.append(current[api[item_id].uniqueid])
|
to_add.setdefault(sensor_config["platform"], []).append(
|
||||||
else:
|
current[api[item_id].uniqueid]
|
||||||
new_sensors.append(current[api[item_id].uniqueid])
|
)
|
||||||
|
|
||||||
self.bridge.hass.async_create_task(
|
self.bridge.hass.async_create_task(
|
||||||
remove_devices(
|
remove_devices(
|
||||||
|
@ -140,10 +139,8 @@ class SensorManager:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if new_sensors:
|
for platform in to_add:
|
||||||
self._component_add_entities[False](new_sensors)
|
self._component_add_entities[platform](to_add[platform])
|
||||||
if new_binary_sensors:
|
|
||||||
self._component_add_entities[True](new_binary_sensors)
|
|
||||||
|
|
||||||
|
|
||||||
class GenericHueSensor(entity.Entity):
|
class GenericHueSensor(entity.Entity):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue