From 6e0aca49affda1101100cc07b3b013e729012dd6 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Thu, 10 Jun 2021 19:15:01 +0200 Subject: [PATCH] Replace properties with attr in Axis integration (#51686) --- homeassistant/components/axis/axis_base.py | 35 ++++++------------- .../components/axis/binary_sensor.py | 9 ++--- homeassistant/components/axis/camera.py | 7 ++-- homeassistant/components/axis/light.py | 16 +++------ homeassistant/components/axis/switch.py | 17 ++++----- 5 files changed, 27 insertions(+), 57 deletions(-) diff --git a/homeassistant/components/axis/axis_base.py b/homeassistant/components/axis/axis_base.py index 3e2b1a48eb7..a652aeb6df8 100644 --- a/homeassistant/components/axis/axis_base.py +++ b/homeassistant/components/axis/axis_base.py @@ -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}" diff --git a/homeassistant/components/axis/binary_sensor.py b/homeassistant/components/axis/binary_sensor.py index 222a356d4f9..7ef3838b1f7 100644 --- a/homeassistant/components/axis/binary_sensor.py +++ b/homeassistant/components/axis/binary_sensor.py @@ -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 diff --git a/homeassistant/components/axis/camera.py b/homeassistant/components/axis/camera.py index cf2634b8f3a..bd0cd46a181 100644 --- a/homeassistant/components/axis/camera.py +++ b/homeassistant/components/axis/camera.py @@ -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.""" diff --git a/homeassistant/components/axis/light.py b/homeassistant/components/axis/light.py index 17b48e7b232..ced795882e1 100644 --- a/homeassistant/components/axis/light.py +++ b/homeassistant/components/axis/light.py @@ -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 diff --git a/homeassistant/components/axis/switch.py b/homeassistant/components/axis/switch.py index e509716fc1f..3a23c3202df 100644 --- a/homeassistant/components/axis/switch.py +++ b/homeassistant/components/axis/switch.py @@ -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