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 <joostlek@outlook.com>

* Apply suggestions from code review

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
J. Nick Koston 2023-09-14 21:28:59 -05:00 committed by GitHub
parent 6a9c9ca735
commit b68ceb3ce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 64 deletions

View File

@ -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,

View File

@ -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 = {

View File

@ -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),
)
)