Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axis devices support device registry #22367

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions homeassistant/components/axis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ async def async_setup_entry(hass, config_entry):

hass.data[DOMAIN][device.serial] = device

await device.async_update_device_registry()

hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, device.shutdown)

return True
Expand Down
13 changes: 13 additions & 0 deletions homeassistant/components/axis/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,20 @@ def device_class(self):
"""Return the class of the event."""
return self.event.event_class

@property
def unique_id(self):
"""Return a unique identifier for this device."""
return '{}-{}-{}'.format(
self.device.serial, self.event.topic, self.event.id)

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def device_info(self):
"""Return a device description for device registry."""
return {
'identifiers': {(AXIS_DOMAIN, self.device.serial)}
}
12 changes: 12 additions & 0 deletions homeassistant/components/axis/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ def _new_ip(self, host):
"""Set new IP for video stream."""
self._mjpeg_url = AXIS_VIDEO.format(host, self.port)
self._still_image_url = AXIS_IMAGE.format(host, self.port)

@property
def unique_id(self):
"""Return a unique identifier for this device."""
return '{}-camera'.format(self.device.serial)

@property
def device_info(self):
"""Return a device description for device registry."""
return {
'identifiers': {(AXIS_DOMAIN, self.device.serial)}
}
17 changes: 16 additions & 1 deletion homeassistant/components/axis/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
CONF_USERNAME)
from homeassistant.core import callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.helpers.dispatcher import async_dispatcher_send

from .const import CONF_CAMERA, CONF_EVENTS, CONF_MODEL, LOGGER
from .const import CONF_CAMERA, CONF_EVENTS, CONF_MODEL, DOMAIN, LOGGER
from .errors import AuthenticationRequired, CannotConnect


Expand Down Expand Up @@ -49,6 +50,20 @@ def serial(self):
"""Return the mac of this device."""
return self.config_entry.data[CONF_MAC]

async def async_update_device_registry(self):
"""Update device registry."""
device_registry = await \
self.hass.helpers.device_registry.async_get_registry()
device_registry.async_get_or_create(
config_entry_id=self.config_entry.entry_id,
connections={(CONNECTION_NETWORK_MAC, self.serial)},
identifiers={(DOMAIN, self.serial)},
manufacturer='Axis Communications AB',
model="{} {}".format(self.model, self.product_type),
name=self.name,
sw_version=self.fw_version
)

async def async_setup(self):
"""Set up the device."""
from axis.vapix import VAPIX_FW_VERSION, VAPIX_PROD_TYPE
Expand Down
1 change: 1 addition & 0 deletions tests/components/axis/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async def test_setup_entry(hass):

mock_device = Mock()
mock_device.async_setup.return_value = mock_coro(True)
mock_device.async_update_device_registry.return_value = mock_coro(True)
mock_device.serial.return_value = '1'

with patch.object(axis, 'AxisNetworkDevice') as mock_device_class, \
Expand Down