2019-04-18 05:13:03 +00:00
|
|
|
"""Support for the Philips Hue sensors as a platform."""
|
2021-05-23 03:32:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-04-18 05:13:03 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
2021-05-23 03:32:41 +00:00
|
|
|
from typing import Any
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2019-10-28 15:45:08 +00:00
|
|
|
from aiohue import AiohueException, Unauthorized
|
2019-10-23 05:58:57 +00:00
|
|
|
from aiohue.sensors import TYPE_ZLL_PRESENCE
|
2019-04-18 05:13:03 +00:00
|
|
|
import async_timeout
|
|
|
|
|
2021-05-07 21:04:00 +00:00
|
|
|
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT
|
2020-01-31 22:47:40 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers import debounce, entity
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
2019-04-18 05:13:03 +00:00
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
from .const import REQUEST_REFRESH_DELAY
|
2019-10-18 22:41:11 +00:00
|
|
|
from .helpers import remove_devices
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
from .hue_event import EVENT_CONFIG_MAP
|
|
|
|
from .sensor_device import GenericHueDevice
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2021-05-23 03:32:41 +00:00
|
|
|
SENSOR_CONFIG_MAP: dict[str, Any] = {}
|
2019-04-18 05:13:03 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _device_id(aiohue_sensor):
|
|
|
|
# Work out the shared device ID, as described below
|
|
|
|
device_id = aiohue_sensor.uniqueid
|
|
|
|
if device_id and len(device_id) > 23:
|
|
|
|
device_id = device_id[:23]
|
|
|
|
return device_id
|
|
|
|
|
|
|
|
|
|
|
|
class SensorManager:
|
|
|
|
"""Class that handles registering and updating Hue sensor entities.
|
|
|
|
|
|
|
|
Intended to be a singleton.
|
|
|
|
"""
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
def __init__(self, bridge):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Initialize the sensor manager."""
|
|
|
|
self.bridge = bridge
|
|
|
|
self._component_add_entities = {}
|
2020-01-31 22:47:40 +00:00
|
|
|
self.current = {}
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
self.current_events = {}
|
|
|
|
|
|
|
|
self._enabled_platforms = ("binary_sensor", "sensor")
|
2020-01-31 22:47:40 +00:00
|
|
|
self.coordinator = DataUpdateCoordinator(
|
|
|
|
bridge.hass,
|
|
|
|
_LOGGER,
|
2020-02-06 17:29:29 +00:00
|
|
|
name="sensor",
|
|
|
|
update_method=self.async_update_data,
|
|
|
|
update_interval=self.SCAN_INTERVAL,
|
|
|
|
request_refresh_debouncer=debounce.Debouncer(
|
|
|
|
bridge.hass, _LOGGER, cooldown=REQUEST_REFRESH_DELAY, immediate=True
|
|
|
|
),
|
2020-01-31 22:47:40 +00:00
|
|
|
)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
async def async_update_data(self):
|
|
|
|
"""Update sensor data."""
|
|
|
|
try:
|
|
|
|
with async_timeout.timeout(4):
|
|
|
|
return await self.bridge.async_request_call(
|
2020-02-08 21:20:37 +00:00
|
|
|
self.bridge.api.sensors.update
|
2020-01-31 22:47:40 +00:00
|
|
|
)
|
2020-08-28 11:50:32 +00:00
|
|
|
except Unauthorized as err:
|
2020-01-31 22:47:40 +00:00
|
|
|
await self.bridge.handle_unauthorized_error()
|
2020-08-28 11:50:32 +00:00
|
|
|
raise UpdateFailed("Unauthorized") from err
|
2020-03-04 16:05:46 +00:00
|
|
|
except AiohueException as err:
|
2020-08-28 11:50:32 +00:00
|
|
|
raise UpdateFailed(f"Hue error: {err}") from err
|
2020-01-31 22:47:40 +00:00
|
|
|
|
2020-03-13 01:31:39 +00:00
|
|
|
async def async_register_component(self, platform, async_add_entities):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Register async_add_entities methods for components."""
|
2020-03-13 01:31:39 +00:00
|
|
|
self._component_add_entities[platform] = async_add_entities
|
2019-04-18 05:13:03 +00:00
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
if len(self._component_add_entities) < len(self._enabled_platforms):
|
|
|
|
_LOGGER.debug("Aborting start with %s, waiting for the rest", platform)
|
2019-04-18 05:13:03 +00:00
|
|
|
return
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
# We have all components available, start the updating.
|
|
|
|
self.bridge.reset_jobs.append(
|
2020-04-03 18:15:42 +00:00
|
|
|
self.coordinator.async_add_listener(self.async_update_items)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-01-31 22:47:40 +00:00
|
|
|
await self.coordinator.async_refresh()
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
@callback
|
|
|
|
def async_update_items(self):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Update sensors from the bridge."""
|
|
|
|
api = self.bridge.api.sensors
|
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
if len(self._component_add_entities) < len(self._enabled_platforms):
|
2019-10-28 15:45:08 +00:00
|
|
|
return
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-03-13 01:31:39 +00:00
|
|
|
to_add = {}
|
2019-04-18 05:13:03 +00:00
|
|
|
primary_sensor_devices = {}
|
2020-01-31 22:47:40 +00:00
|
|
|
current = self.current
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
# Physical Hue motion sensors present as three sensors in the API: a
|
|
|
|
# presence sensor, a temperature sensor, and a light level sensor. Of
|
|
|
|
# these, only the presence sensor is assigned the user-friendly name
|
|
|
|
# that the user has given to the device. Each of these sensors is
|
|
|
|
# linked by a common device_id, which is the first twenty-three
|
|
|
|
# characters of the unique id (then followed by a hyphen and an ID
|
|
|
|
# specific to the individual sensor).
|
|
|
|
#
|
|
|
|
# To set up neat values, and assign the sensor entities to the same
|
|
|
|
# device, we first, iterate over all the sensors and find the Hue
|
|
|
|
# presence sensors, then iterate over all the remaining sensors -
|
|
|
|
# finding the remaining ones that may or may not be related to the
|
|
|
|
# presence sensors.
|
|
|
|
for item_id in api:
|
2019-10-23 05:58:57 +00:00
|
|
|
if api[item_id].type != TYPE_ZLL_PRESENCE:
|
2019-04-18 05:13:03 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
primary_sensor_devices[_device_id(api[item_id])] = api[item_id]
|
|
|
|
|
|
|
|
# Iterate again now we have all the presence sensors, and add the
|
|
|
|
# related sensors with nice names where appropriate.
|
|
|
|
for item_id in api:
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
uniqueid = api[item_id].uniqueid
|
|
|
|
if current.get(uniqueid, self.current_events.get(uniqueid)) is not None:
|
2019-04-18 05:13:03 +00:00
|
|
|
continue
|
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
sensor_type = api[item_id].type
|
|
|
|
|
|
|
|
# Check for event generator devices
|
|
|
|
event_config = EVENT_CONFIG_MAP.get(sensor_type)
|
|
|
|
if event_config is not None:
|
|
|
|
base_name = api[item_id].name
|
|
|
|
name = event_config["name_format"].format(base_name)
|
|
|
|
new_event = event_config["class"](api[item_id], name, self.bridge)
|
|
|
|
self.bridge.hass.async_create_task(
|
|
|
|
new_event.async_update_device_registry()
|
|
|
|
)
|
|
|
|
self.current_events[uniqueid] = new_event
|
|
|
|
|
|
|
|
sensor_config = SENSOR_CONFIG_MAP.get(sensor_type)
|
2019-04-18 05:13:03 +00:00
|
|
|
if sensor_config is None:
|
|
|
|
continue
|
|
|
|
|
|
|
|
base_name = api[item_id].name
|
2019-07-31 19:25:30 +00:00
|
|
|
primary_sensor = primary_sensor_devices.get(_device_id(api[item_id]))
|
2019-04-18 05:13:03 +00:00
|
|
|
if primary_sensor is not None:
|
|
|
|
base_name = primary_sensor.name
|
|
|
|
name = sensor_config["name_format"].format(base_name)
|
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
current[uniqueid] = sensor_config["class"](
|
2019-07-31 19:25:30 +00:00
|
|
|
api[item_id], name, self.bridge, primary_sensor=primary_sensor
|
|
|
|
)
|
2020-03-13 01:31:39 +00:00
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
to_add.setdefault(sensor_config["platform"], []).append(current[uniqueid])
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
self.bridge.hass.async_create_task(
|
|
|
|
remove_devices(
|
2020-08-27 11:56:20 +00:00
|
|
|
self.bridge,
|
|
|
|
[value.uniqueid for value in api.values()],
|
|
|
|
current,
|
2020-01-31 22:47:40 +00:00
|
|
|
)
|
2019-10-18 22:41:11 +00:00
|
|
|
)
|
|
|
|
|
2021-07-15 04:44:57 +00:00
|
|
|
for platform, value in to_add.items():
|
|
|
|
self._component_add_entities[platform](value)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
class GenericHueSensor(GenericHueDevice, entity.Entity):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Representation of a Hue sensor."""
|
|
|
|
|
|
|
|
should_poll = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if sensor is available."""
|
2020-02-06 17:29:29 +00:00
|
|
|
return self.bridge.sensor_manager.coordinator.last_update_success and (
|
Fire events for hue remote buttons pressed (#33277)
* Add remote platform to hue integration
supporting ZGPSwitch, ZLLSwitch and ZLLRotary switches.
* Ported from custom component Hue-remotes-HASS from @robmarkcole
* Add options flow for hue, to toggle handling of sensors and remotes
* Sensors are enabled by default, and remotes are disabled,
to not generate any breaking change for existent users.
Also, when linking a new bridge these defaults are used,
so unless going explicitly to the Options menu,
the old behavior is preserved.
* SensorManager stores the enabled platforms and ignores everything else.
* Bridge is created with flags for `add_sensors` and `add_remotes`,
and uses them to forward entry setup to only the enabled platforms.
* Update listener removes disabled kinds of devices when options are changed,
so device list is in sync with options, and disabled kinds disappear from HA,
leaving the enable/disable entity option for individual devices.
* Fix hue bridge mock with new parameters
* Revert changes in hue bridge mock
* Remove OptionsFlow and platform flags
* Extract `GenericHueDevice` from `GenericHueSensor`
to use it as base class for all hue devices, including those without any entity,
like remotes without battery.
* Add `HueBattery` sensor for battery powered remotes
and generate entities for TYPE_ZLL_ROTARY and TYPE_ZLL_SWITCH remotes.
* Remove remote platform
* Add HueEvent class to fire events for button presses
* Use `sensor.lastupdated` string to control state changes
* Event data includes:
- "id", as pretty name of the remote
- "unique_id" of the remote device
- "event", with the raw code of the pressed button
('buttonevent' or 'rotaryevent' property)
- "last_updated", with the bridge timestamp for the button press
* Register ZGP_SWITCH, ZLL_SWITCH, ZLL_ROTARY remotes
* fix removal
* Exclude W0611
* Extract GenericHueDevice to its own module
and solve import tree, also fixing lint in CI
* Store registered events to do not repeat device reg
* Minor cleaning
* Add tests for hue_event and battery entities for hue remotes
2020-03-31 17:27:30 +00:00
|
|
|
self.bridge.allow_unreachable
|
|
|
|
# remotes like Hue Tap (ZGPSwitchSensor) have no _reachability_
|
|
|
|
or self.sensor.config.get("reachable", True)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2021-05-07 21:04:00 +00:00
|
|
|
@property
|
|
|
|
def state_class(self):
|
|
|
|
"""Return the state class of this entity, from STATE_CLASSES, if any."""
|
|
|
|
return STATE_CLASS_MEASUREMENT
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""When entity is added to hass."""
|
2021-05-14 20:39:57 +00:00
|
|
|
await super().async_added_to_hass()
|
2020-04-03 18:15:42 +00:00
|
|
|
self.async_on_remove(
|
|
|
|
self.bridge.sensor_manager.coordinator.async_add_listener(
|
|
|
|
self.async_write_ha_state
|
|
|
|
)
|
2020-01-31 22:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update the entity.
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
Only used by the generic entity update service.
|
2019-04-18 05:13:03 +00:00
|
|
|
"""
|
2020-02-04 22:57:15 +00:00
|
|
|
await self.bridge.sensor_manager.coordinator.async_request_refresh()
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GenericZLLSensor(GenericHueSensor):
|
|
|
|
"""Representation of a Hue-brand, physical sensor."""
|
|
|
|
|
|
|
|
@property
|
2021-03-11 15:57:47 +00:00
|
|
|
def extra_state_attributes(self):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Return the device state attributes."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"battery_level": self.sensor.battery}
|