Light control support to Axis devices (#36611)
* IR light support to Axis devices * Change how to read light state * Add tests * Bump dependency to v32 * Assert variables passed to set_intensity
This commit is contained in:
1 parent
e92e26b73a
commit
02e03340df
8 files changed
+301
-8
No files matched your search
@@ -42,7 +42,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Add binary sensor from Axis device."""
|
||||
event = device.api.event[event_id]
|
||||
|
||||
if event.CLASS != CLASS_OUTPUT:
|
||||
if event.CLASS != CLASS_OUTPUT and not (
|
||||
event.CLASS == CLASS_LIGHT and event.TYPE == "Light"
|
||||
):
|
||||
async_add_entities([AxisBinarySensor(event, device)], True)
|
||||
|
||||
device.listeners.append(
|
||||
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
|
||||
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
|
||||
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
@@ -19,4 +20,4 @@ DEFAULT_EVENTS = True
|
||||
DEFAULT_STREAM_PROFILE = "No stream profile"
|
||||
DEFAULT_TRIGGER_TIME = 0
|
||||
|
||||
PLATFORMS = [BINARY_SENSOR_DOMAIN, CAMERA_DOMAIN, SWITCH_DOMAIN]
|
||||
PLATFORMS = [BINARY_SENSOR_DOMAIN, CAMERA_DOMAIN, LIGHT_DOMAIN, SWITCH_DOMAIN]
|
||||
@@ -0,0 +1,116 @@
|
||||
"""Support for Axis lights."""
|
||||
|
||||
from axis.event_stream import CLASS_LIGHT
|
||||
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS,
|
||||
SUPPORT_BRIGHTNESS,
|
||||
LightEntity,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from .axis_base import AxisEventBase
|
||||
from .const import DOMAIN as AXIS_DOMAIN
|
||||
|
||||
|
||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
"""Set up a Axis light."""
|
||||
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
|
||||
|
||||
if not device.api.vapix.light_control:
|
||||
return
|
||||
|
||||
@callback
|
||||
def async_add_sensor(event_id):
|
||||
"""Add light from Axis device."""
|
||||
event = device.api.event[event_id]
|
||||
|
||||
if event.CLASS == CLASS_LIGHT and event.TYPE == "Light":
|
||||
async_add_entities([AxisLight(event, device)], True)
|
||||
|
||||
device.listeners.append(
|
||||
async_dispatcher_connect(hass, device.signal_new_event, async_add_sensor)
|
||||
)
|
||||
|
||||
|
||||
class AxisLight(AxisEventBase, LightEntity):
|
||||
"""Representation of a light Axis event."""
|
||||
|
||||
def __init__(self, event, device):
|
||||
"""Initialize the Axis light."""
|
||||
super().__init__(event, device)
|
||||
|
||||
self.light_id = f"led{self.event.id}"
|
||||
|
||||
self.current_intensity = 0
|
||||
self.max_intensity = 0
|
||||
|
||||
self._features = SUPPORT_BRIGHTNESS
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Subscribe lights events."""
|
||||
await super().async_added_to_hass()
|
||||
|
||||
def get_light_capabilities():
|
||||
"""Get light capabilities."""
|
||||
current_intensity = self.device.api.vapix.light_control.get_current_intensity(
|
||||
self.light_id
|
||||
)
|
||||
self.current_intensity = current_intensity["data"]["intensity"]
|
||||
|
||||
max_intensity = self.device.api.vapix.light_control.get_valid_intensity(
|
||||
self.light_id
|
||||
)
|
||||
self.max_intensity = max_intensity["data"]["ranges"][0]["high"]
|
||||
|
||||
await self.hass.async_add_executor_job(get_light_capabilities)
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
return self._features
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the light."""
|
||||
light_type = self.device.api.vapix.light_control[self.light_id].light_type
|
||||
return f"{self.device.name} {light_type} {self.event.TYPE} {self.event.id}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if light is on."""
|
||||
return self.event.is_tripped
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""Return the brightness of this light between 0..255."""
|
||||
return int((self.current_intensity / self.max_intensity) * 255)
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn on light."""
|
||||
if not self.is_on:
|
||||
self.device.api.vapix.light_control.activate_light(self.light_id)
|
||||
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
intensity = int((kwargs[ATTR_BRIGHTNESS] / 255) * self.max_intensity)
|
||||
self.device.api.vapix.light_control.set_manual_intensity(
|
||||
self.light_id, intensity
|
||||
)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn off light."""
|
||||
if self.is_on:
|
||||
self.device.api.vapix.light_control.deactivate_light(self.light_id)
|
||||
|
||||
def update(self):
|
||||
"""Update brightness."""
|
||||
current_intensity = self.device.api.vapix.light_control.get_current_intensity(
|
||||
self.light_id
|
||||
)
|
||||
self.current_intensity = current_intensity["data"]["intensity"]
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Brightness needs polling."""
|
||||
return True
|
||||
@@ -3,7 +3,7 @@
|
||||
"name": "Axis",
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/axis",
|
||||
"requirements": ["axis==31"],
|
||||
"requirements": ["axis==32"],
|
||||
"zeroconf": ["_axis-video._tcp.local."],
|
||||
"after_dependencies": ["mqtt"],
|
||||
"codeowners": ["@Kane610"]
|
||||
|
||||
Reference in new issue
Block a user