2019-04-18 05:13:03 +00:00
|
|
|
"""Hue sensor entities."""
|
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 aiohue.sensors import (
|
|
|
|
TYPE_ZLL_LIGHTLEVEL,
|
|
|
|
TYPE_ZLL_ROTARY,
|
|
|
|
TYPE_ZLL_SWITCH,
|
|
|
|
TYPE_ZLL_TEMPERATURE,
|
|
|
|
)
|
2019-10-23 05:58:57 +00:00
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2019-04-18 05:13:03 +00:00
|
|
|
from homeassistant.const import (
|
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
|
|
|
DEVICE_CLASS_BATTERY,
|
2019-07-31 19:25:30 +00:00
|
|
|
DEVICE_CLASS_ILLUMINANCE,
|
|
|
|
DEVICE_CLASS_TEMPERATURE,
|
2020-09-23 18:48:01 +00:00
|
|
|
LIGHT_LUX,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2019-07-31 19:25:30 +00:00
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
from .const import DOMAIN as HUE_DOMAIN
|
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 .sensor_base import SENSOR_CONFIG_MAP, GenericHueSensor, GenericZLLSensor
|
2020-01-31 22:47:40 +00:00
|
|
|
|
2019-04-18 05:13:03 +00:00
|
|
|
LIGHT_LEVEL_NAME_FORMAT = "{} light level"
|
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
|
|
|
REMOTE_NAME_FORMAT = "{} battery level"
|
2019-04-18 05:13:03 +00:00
|
|
|
TEMPERATURE_NAME_FORMAT = "{} temperature"
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Defer sensor setup to the shared sensor module."""
|
2021-05-07 12:24:47 +00:00
|
|
|
bridge = hass.data[HUE_DOMAIN][config_entry.entry_id]
|
|
|
|
|
|
|
|
if not bridge.sensor_manager:
|
|
|
|
return
|
|
|
|
|
|
|
|
await bridge.sensor_manager.async_register_component("sensor", async_add_entities)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class GenericHueGaugeSensorEntity(GenericZLLSensor, SensorEntity):
|
2019-04-18 05:13:03 +00:00
|
|
|
"""Parent class for all 'gauge' Hue device sensors."""
|
|
|
|
|
|
|
|
|
|
|
|
class HueLightLevel(GenericHueGaugeSensorEntity):
|
|
|
|
"""The light level sensor entity for a Hue motion sensor device."""
|
|
|
|
|
|
|
|
device_class = DEVICE_CLASS_ILLUMINANCE
|
2020-09-23 18:48:01 +00:00
|
|
|
unit_of_measurement = LIGHT_LUX
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2019-04-27 20:27:17 +00:00
|
|
|
if self.sensor.lightlevel is None:
|
|
|
|
return None
|
|
|
|
|
2019-04-23 04:28:40 +00:00
|
|
|
# https://developers.meethue.com/develop/hue-api/supported-devices/#clip_zll_lightlevel
|
|
|
|
# Light level in 10000 log10 (lux) +1 measured by sensor. Logarithm
|
|
|
|
# scale used because the human eye adjusts to light levels and small
|
|
|
|
# changes at low lux levels are more noticeable than at high lux
|
|
|
|
# levels.
|
2019-06-11 17:41:20 +00:00
|
|
|
return round(float(10 ** ((self.sensor.lightlevel - 1) / 10000)), 2)
|
2019-04-18 05:13:03 +00:00
|
|
|
|
|
|
|
@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."""
|
2021-03-11 15:57:47 +00:00
|
|
|
attributes = super().extra_state_attributes
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes.update(
|
|
|
|
{
|
|
|
|
"lightlevel": self.sensor.lightlevel,
|
|
|
|
"daylight": self.sensor.daylight,
|
|
|
|
"dark": self.sensor.dark,
|
|
|
|
"threshold_dark": self.sensor.tholddark,
|
|
|
|
"threshold_offset": self.sensor.tholdoffset,
|
|
|
|
}
|
|
|
|
)
|
2019-04-18 05:13:03 +00:00
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
|
|
class HueTemperature(GenericHueGaugeSensorEntity):
|
|
|
|
"""The temperature sensor entity for a Hue motion sensor device."""
|
|
|
|
|
|
|
|
device_class = DEVICE_CLASS_TEMPERATURE
|
|
|
|
unit_of_measurement = TEMP_CELSIUS
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the device."""
|
2019-04-27 20:27:17 +00:00
|
|
|
if self.sensor.temperature is None:
|
|
|
|
return None
|
|
|
|
|
2019-04-18 05:13:03 +00:00
|
|
|
return self.sensor.temperature / 100
|
2020-01-31 22:47:40 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class HueBattery(GenericHueSensor, SensorEntity):
|
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
|
|
|
"""Battery class for when a batt-powered device is only represented as an event."""
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique identifier for this device."""
|
|
|
|
return f"{self.sensor.uniqueid}-battery"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the battery."""
|
|
|
|
return self.sensor.battery
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of the sensor."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
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
|
|
|
|
|
|
|
|
2020-01-31 22:47:40 +00:00
|
|
|
SENSOR_CONFIG_MAP.update(
|
|
|
|
{
|
|
|
|
TYPE_ZLL_LIGHTLEVEL: {
|
2020-03-13 01:31:39 +00:00
|
|
|
"platform": "sensor",
|
2020-01-31 22:47:40 +00:00
|
|
|
"name_format": LIGHT_LEVEL_NAME_FORMAT,
|
|
|
|
"class": HueLightLevel,
|
|
|
|
},
|
|
|
|
TYPE_ZLL_TEMPERATURE: {
|
2020-03-13 01:31:39 +00:00
|
|
|
"platform": "sensor",
|
2020-01-31 22:47:40 +00:00
|
|
|
"name_format": TEMPERATURE_NAME_FORMAT,
|
|
|
|
"class": HueTemperature,
|
|
|
|
},
|
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
|
|
|
TYPE_ZLL_SWITCH: {
|
|
|
|
"platform": "sensor",
|
|
|
|
"name_format": REMOTE_NAME_FORMAT,
|
|
|
|
"class": HueBattery,
|
|
|
|
},
|
|
|
|
TYPE_ZLL_ROTARY: {
|
|
|
|
"platform": "sensor",
|
|
|
|
"name_format": REMOTE_NAME_FORMAT,
|
|
|
|
"class": HueBattery,
|
|
|
|
},
|
2020-01-31 22:47:40 +00:00
|
|
|
}
|
|
|
|
)
|