Replace Starline horn switch with button (#105728)

pull/105722/head
Nikolay Vasilchuk 2023-12-14 17:00:23 +03:00 committed by GitHub
parent 351b07b14d
commit 356ad52d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 2 deletions

View File

@ -1216,6 +1216,7 @@ omit =
homeassistant/components/starline/__init__.py
homeassistant/components/starline/account.py
homeassistant/components/starline/binary_sensor.py
homeassistant/components/starline/button.py
homeassistant/components/starline/device_tracker.py
homeassistant/components/starline/entity.py
homeassistant/components/starline/lock.py

View File

@ -0,0 +1,57 @@
"""Support for StarLine button."""
from __future__ import annotations
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .account import StarlineAccount, StarlineDevice
from .const import DOMAIN
from .entity import StarlineEntity
BUTTON_TYPES: tuple[ButtonEntityDescription, ...] = (
ButtonEntityDescription(
key="poke",
translation_key="horn",
icon="mdi:bullhorn-outline",
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the StarLine button."""
account: StarlineAccount = hass.data[DOMAIN][entry.entry_id]
entities = []
for device in account.api.devices.values():
if device.support_state:
for description in BUTTON_TYPES:
entities.append(StarlineButton(account, device, description))
async_add_entities(entities)
class StarlineButton(StarlineEntity, ButtonEntity):
"""Representation of a StarLine button."""
entity_description: ButtonEntityDescription
def __init__(
self,
account: StarlineAccount,
device: StarlineDevice,
description: ButtonEntityDescription,
) -> None:
"""Initialize the button."""
super().__init__(account, device, description.key)
self.entity_description = description
@property
def available(self):
"""Return True if entity is available."""
return super().available and self._device.online
def press(self):
"""Press the button."""
self._account.api.set_car_state(self._device.device_id, self._key, True)

View File

@ -7,10 +7,11 @@ _LOGGER = logging.getLogger(__package__)
DOMAIN = "starline"
PLATFORMS = [
Platform.DEVICE_TRACKER,
Platform.BINARY_SENSOR,
Platform.SENSOR,
Platform.BUTTON,
Platform.DEVICE_TRACKER,
Platform.LOCK,
Platform.SENSOR,
Platform.SWITCH,
]

View File

@ -105,6 +105,17 @@
"horn": {
"name": "Horn"
}
},
"button": {
"horn": {
"name": "Horn"
}
}
},
"issues": {
"deprecated_horn_switch": {
"title": "The Starline Horn switch entity is being removed",
"description": "Using the Horn switch is now deprecated and will be removed in a future version of Home Assistant.\n\nPlease adjust any automations or scripts that use Horn switch entity to instead use the Horn button entity."
}
},
"services": {

View File

@ -8,6 +8,7 @@ from homeassistant.components.switch import SwitchEntity, SwitchEntityDescriptio
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, create_issue
from .account import StarlineAccount, StarlineDevice
from .const import DOMAIN
@ -48,6 +49,7 @@ SWITCH_TYPES: tuple[StarlineSwitchEntityDescription, ...] = (
icon_on="mdi:access-point-network",
icon_off="mdi:access-point-network-off",
),
# Deprecated and should be removed in 2024.8
StarlineSwitchEntityDescription(
key="poke",
translation_key="horn",
@ -119,6 +121,16 @@ class StarlineSwitch(StarlineEntity, SwitchEntity):
def turn_on(self, **kwargs: Any) -> None:
"""Turn the entity on."""
if self._key == "poke":
create_issue(
self.hass,
DOMAIN,
"deprecated_horn_switch",
breaks_in_ha_version="2024.8.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_horn_switch",
)
self._account.api.set_car_state(self._device.device_id, self._key, True)
def turn_off(self, **kwargs: Any) -> None: