Replace properties with attr in Axis integration (#51686)

pull/51727/head
Robert Svensson 2021-06-10 19:15:01 +02:00 committed by GitHub
parent 76c3058d15
commit 6e0aca49af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 57 deletions

View File

@ -1,5 +1,6 @@
"""Base classes for Axis entities."""
from homeassistant.const import ATTR_IDENTIFIERS
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
@ -14,6 +15,8 @@ class AxisEntityBase(Entity):
"""Initialize the Axis event."""
self.device = device
self._attr_device_info = {ATTR_IDENTIFIERS: {(AXIS_DOMAIN, device.unique_id)}}
async def async_added_to_hass(self):
"""Subscribe device events."""
self.async_on_remove(
@ -27,11 +30,6 @@ class AxisEntityBase(Entity):
"""Return True if device is available."""
return self.device.available
@property
def device_info(self):
"""Return a device description for device registry."""
return {"identifiers": {(AXIS_DOMAIN, self.device.unique_id)}}
@callback
def update_callback(self, no_delay=None):
"""Update the entities state."""
@ -41,11 +39,18 @@ class AxisEntityBase(Entity):
class AxisEventBase(AxisEntityBase):
"""Base common to all Axis entities from event stream."""
_attr_should_poll = False
def __init__(self, event, device):
"""Initialize the Axis event."""
super().__init__(device)
self.event = event
self._attr_name = f"{device.name} {event.TYPE} {event.id}"
self._attr_unique_id = f"{device.unique_id}-{event.topic}-{event.id}"
self._attr_device_class = event.CLASS
async def async_added_to_hass(self) -> None:
"""Subscribe sensors events."""
self.event.register_callback(self.update_callback)
@ -54,23 +59,3 @@ class AxisEventBase(AxisEntityBase):
async def async_will_remove_from_hass(self) -> None:
"""Disconnect device object when removed."""
self.event.remove_callback(self.update_callback)
@property
def device_class(self):
"""Return the class of the event."""
return self.event.CLASS
@property
def name(self):
"""Return the name of the event."""
return f"{self.device.name} {self.event.TYPE} {self.event.id}"
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def unique_id(self):
"""Return a unique identifier for this device."""
return f"{self.device.unique_id}-{self.event.topic}-{self.event.id}"

View File

@ -66,6 +66,8 @@ class AxisBinarySensor(AxisEventBase, BinarySensorEntity):
super().__init__(event, device)
self.cancel_scheduled_update = None
self._attr_device_class = DEVICE_CLASS.get(self.event.CLASS)
@callback
def update_callback(self, no_delay=False):
"""Update the sensor's state, if needed.
@ -126,9 +128,4 @@ class AxisBinarySensor(AxisEventBase, BinarySensorEntity):
):
return f"{self.device.name} {self.event.TYPE} {event_data[self.event.id].name}"
return super().name
@property
def device_class(self):
"""Return the class of the sensor."""
return DEVICE_CLASS.get(self.event.CLASS)
return self._attr_name

View File

@ -51,6 +51,8 @@ class AxisCamera(AxisEntityBase, MjpegCamera):
}
MjpegCamera.__init__(self, config)
self._attr_unique_id = f"{device.unique_id}-camera"
async def async_added_to_hass(self):
"""Subscribe camera events."""
self.async_on_remove(
@ -71,11 +73,6 @@ class AxisCamera(AxisEntityBase, MjpegCamera):
self._mjpeg_url = self.mjpeg_source
self._still_image_url = self.image_source
@property
def unique_id(self) -> str:
"""Return a unique identifier for this device."""
return f"{self.device.unique_id}-camera"
@property
def image_source(self) -> str:
"""Return still image URL for device."""

View File

@ -40,6 +40,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class AxisLight(AxisEventBase, LightEntity):
"""Representation of a light Axis event."""
_attr_should_poll = True
def __init__(self, event, device):
"""Initialize the Axis light."""
super().__init__(event, device)
@ -49,6 +51,9 @@ class AxisLight(AxisEventBase, LightEntity):
self.current_intensity = 0
self.max_intensity = 0
light_type = device.api.vapix.light_control[self.light_id].light_type
self._attr_name = f"{device.name} {light_type} {event.TYPE} {event.id}"
self._attr_supported_color_modes = {COLOR_MODE_BRIGHTNESS}
self._attr_color_mode = COLOR_MODE_BRIGHTNESS
@ -68,12 +73,6 @@ class AxisLight(AxisEventBase, LightEntity):
)
self.max_intensity = max_intensity["data"]["ranges"][0]["high"]
@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."""
@ -108,8 +107,3 @@ class AxisLight(AxisEventBase, LightEntity):
)
)
self.current_intensity = current_intensity["data"]["intensity"]
@property
def should_poll(self):
"""Brightness needs polling."""
return True

View File

@ -30,6 +30,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class AxisSwitch(AxisEventBase, SwitchEntity):
"""Representation of a Axis switch."""
def __init__(self, event, device):
"""Initialize the Axis switch."""
super().__init__(event, device)
if event.id and device.api.vapix.ports[event.id].name:
self._attr_name = f"{device.name} {device.api.vapix.ports[event.id].name}"
@property
def is_on(self):
"""Return true if event is active."""
@ -42,13 +49,3 @@ class AxisSwitch(AxisEventBase, SwitchEntity):
async def async_turn_off(self, **kwargs):
"""Turn off switch."""
await self.device.api.vapix.ports[self.event.id].open()
@property
def name(self):
"""Return the name of the event."""
if self.event.id and self.device.api.vapix.ports[self.event.id].name:
return (
f"{self.device.name} {self.device.api.vapix.ports[self.event.id].name}"
)
return super().name