From b68ceb3ce4ae61127d5200aabd9dda40eb687465 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 14 Sep 2023 21:28:59 -0500 Subject: [PATCH] Use more shorthand attributes in hyperion (#100213) * Use more shorthand attributes in hyperion There are likely some more here, but I only did the safe ones * Update homeassistant/components/hyperion/switch.py Co-authored-by: Joost Lekkerkerker * Apply suggestions from code review --------- Co-authored-by: Joost Lekkerkerker --- homeassistant/components/hyperion/camera.py | 27 ++++++---------- homeassistant/components/hyperion/light.py | 30 +++++------------- homeassistant/components/hyperion/switch.py | 35 +++++++-------------- 3 files changed, 28 insertions(+), 64 deletions(-) diff --git a/homeassistant/components/hyperion/camera.py b/homeassistant/components/hyperion/camera.py index 9c9e509947d..23ce2715140 100644 --- a/homeassistant/components/hyperion/camera.py +++ b/homeassistant/components/hyperion/camera.py @@ -119,7 +119,7 @@ class HyperionCamera(Camera): """Initialize the switch.""" super().__init__() - self._unique_id = get_hyperion_unique_id( + self._attr_unique_id = get_hyperion_unique_id( server_id, instance_num, TYPE_HYPERION_CAMERA ) self._device_id = get_hyperion_device_id(server_id, instance_num) @@ -135,11 +135,13 @@ class HyperionCamera(Camera): self._client_callbacks = { f"{KEY_LEDCOLORS}-{KEY_IMAGE_STREAM}-{KEY_UPDATE}": self._update_imagestream } - - @property - def unique_id(self) -> str: - """Return a unique id for this instance.""" - return self._unique_id + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self._device_id)}, + manufacturer=HYPERION_MANUFACTURER_NAME, + model=HYPERION_MODEL_NAME, + name=instance_name, + configuration_url=hyperion_client.remote_url, + ) @property def is_on(self) -> bool: @@ -231,7 +233,7 @@ class HyperionCamera(Camera): self.async_on_remove( async_dispatcher_connect( self.hass, - SIGNAL_ENTITY_REMOVE.format(self._unique_id), + SIGNAL_ENTITY_REMOVE.format(self._attr_unique_id), functools.partial(self.async_remove, force_remove=True), ) ) @@ -242,17 +244,6 @@ class HyperionCamera(Camera): """Cleanup prior to hass removal.""" self._client.remove_callbacks(self._client_callbacks) - @property - def device_info(self) -> DeviceInfo: - """Return device information.""" - return DeviceInfo( - identifiers={(DOMAIN, self._device_id)}, - manufacturer=HYPERION_MANUFACTURER_NAME, - model=HYPERION_MODEL_NAME, - name=self._instance_name, - configuration_url=self._client.remote_url, - ) - CAMERA_TYPES = { TYPE_HYPERION_CAMERA: HyperionCamera, diff --git a/homeassistant/components/hyperion/light.py b/homeassistant/components/hyperion/light.py index 105e577efad..824d83591ef 100644 --- a/homeassistant/components/hyperion/light.py +++ b/homeassistant/components/hyperion/light.py @@ -132,7 +132,7 @@ class HyperionLight(LightEntity): hyperion_client: client.HyperionClient, ) -> None: """Initialize the light.""" - self._unique_id = self._compute_unique_id(server_id, instance_num) + self._attr_unique_id = self._compute_unique_id(server_id, instance_num) self._device_id = get_hyperion_device_id(server_id, instance_num) self._instance_name = instance_name self._options = options @@ -153,16 +153,18 @@ class HyperionLight(LightEntity): f"{const.KEY_PRIORITIES}-{const.KEY_UPDATE}": self._update_priorities, f"{const.KEY_CLIENT}-{const.KEY_UPDATE}": self._update_client, } + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self._device_id)}, + manufacturer=HYPERION_MANUFACTURER_NAME, + model=HYPERION_MODEL_NAME, + name=self._instance_name, + configuration_url=self._client.remote_url, + ) def _compute_unique_id(self, server_id: str, instance_num: int) -> str: """Compute a unique id for this instance.""" return get_hyperion_unique_id(server_id, instance_num, TYPE_HYPERION_LIGHT) - @property - def entity_registry_enabled_default(self) -> bool: - """Whether or not the entity is enabled by default.""" - return True - @property def brightness(self) -> int: """Return the brightness of this light between 0..255.""" @@ -196,22 +198,6 @@ class HyperionLight(LightEntity): """Return server availability.""" return bool(self._client.has_loaded_state) - @property - def unique_id(self) -> str: - """Return a unique id for this instance.""" - return self._unique_id - - @property - def device_info(self) -> DeviceInfo: - """Return device information.""" - return DeviceInfo( - identifiers={(DOMAIN, self._device_id)}, - manufacturer=HYPERION_MANUFACTURER_NAME, - model=HYPERION_MODEL_NAME, - name=self._instance_name, - configuration_url=self._client.remote_url, - ) - def _get_option(self, key: str) -> Any: """Get a value from the provided options.""" defaults = { diff --git a/homeassistant/components/hyperion/switch.py b/homeassistant/components/hyperion/switch.py index 11e1dc199be..eb7b260a370 100644 --- a/homeassistant/components/hyperion/switch.py +++ b/homeassistant/components/hyperion/switch.py @@ -133,6 +133,8 @@ class HyperionComponentSwitch(SwitchEntity): _attr_entity_category = EntityCategory.CONFIG _attr_should_poll = False _attr_has_entity_name = True + # These component controls are for advanced users and are disabled by default. + _attr_entity_registry_enabled_default = False def __init__( self, @@ -143,7 +145,7 @@ class HyperionComponentSwitch(SwitchEntity): hyperion_client: client.HyperionClient, ) -> None: """Initialize the switch.""" - self._unique_id = _component_to_unique_id( + self._attr_unique_id = _component_to_unique_id( server_id, component_name, instance_num ) self._device_id = get_hyperion_device_id(server_id, instance_num) @@ -154,17 +156,13 @@ class HyperionComponentSwitch(SwitchEntity): self._client_callbacks = { f"{KEY_COMPONENTS}-{KEY_UPDATE}": self._update_components } - - @property - def entity_registry_enabled_default(self) -> bool: - """Whether or not the entity is enabled by default.""" - # These component controls are for advanced users and are disabled by default. - return False - - @property - def unique_id(self) -> str: - """Return a unique id for this instance.""" - return self._unique_id + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self._device_id)}, + manufacturer=HYPERION_MANUFACTURER_NAME, + model=HYPERION_MODEL_NAME, + name=self._instance_name, + configuration_url=self._client.remote_url, + ) @property def is_on(self) -> bool: @@ -179,17 +177,6 @@ class HyperionComponentSwitch(SwitchEntity): """Return server availability.""" return bool(self._client.has_loaded_state) - @property - def device_info(self) -> DeviceInfo: - """Return device information.""" - return DeviceInfo( - identifiers={(DOMAIN, self._device_id)}, - manufacturer=HYPERION_MANUFACTURER_NAME, - model=HYPERION_MODEL_NAME, - name=self._instance_name, - configuration_url=self._client.remote_url, - ) - async def _async_send_set_component(self, value: bool) -> None: """Send a component control request.""" await self._client.async_send_set_component( @@ -219,7 +206,7 @@ class HyperionComponentSwitch(SwitchEntity): self.async_on_remove( async_dispatcher_connect( self.hass, - SIGNAL_ENTITY_REMOVE.format(self._unique_id), + SIGNAL_ENTITY_REMOVE.format(self._attr_unique_id), functools.partial(self.async_remove, force_remove=True), ) )