-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
USe SwitchEntityDescription in switch
- Loading branch information
Showing
1 changed file
with
33 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,52 @@ | ||
"""Switch platform for integration_blueprint.""" | ||
from homeassistant.components.switch import SwitchEntity | ||
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription | ||
|
||
from .const import DEFAULT_NAME, DOMAIN, ICON, SWITCH | ||
from .const import DOMAIN | ||
from .coordinator import BlueprintDataUpdateCoordinator | ||
from .entity import IntegrationBlueprintEntity | ||
|
||
ENTITY_DESCRIPTIONS = ( | ||
SwitchEntityDescription( | ||
key="integration_blueprint", | ||
name="Integration Switch", | ||
icon="mdi:format-quote-close", | ||
), | ||
) | ||
|
||
async def async_setup_entry(hass, entry, async_add_devices): | ||
"""Setup sensor platform.""" | ||
coordinator = hass.data[DOMAIN][entry.entry_id] | ||
async_add_devices([IntegrationBlueprintBinarySwitch(coordinator, entry)]) | ||
async_add_devices( | ||
IntegrationBlueprintSwitch( | ||
coordinator=coordinator, | ||
entity_description=entity_description, | ||
) | ||
for entity_description in ENTITY_DESCRIPTIONS | ||
) | ||
|
||
|
||
class IntegrationBlueprintBinarySwitch(IntegrationBlueprintEntity, SwitchEntity): | ||
class IntegrationBlueprintSwitch(IntegrationBlueprintEntity, SwitchEntity): | ||
"""integration_blueprint switch class.""" | ||
|
||
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument | ||
def __init__( | ||
self, | ||
coordinator: BlueprintDataUpdateCoordinator, | ||
entity_description: SwitchEntityDescription, | ||
) -> None: | ||
super().__init__(coordinator) | ||
self.entity_description = entity_description | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the switch is on.""" | ||
return self.coordinator.data.get("title", "") == "foo" | ||
|
||
async def async_turn_on(self, **kwargs: any): # pylint: disable=unused-argument | ||
"""Turn on the switch.""" | ||
await self.coordinator.api.async_set_title("bar") | ||
await self.coordinator.async_request_refresh() | ||
|
||
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument | ||
async def async_turn_off(self, **kwargs: any): # pylint: disable=unused-argument | ||
"""Turn off the switch.""" | ||
await self.coordinator.api.async_set_title("foo") | ||
await self.coordinator.async_request_refresh() | ||
|
||
@property | ||
def name(self): | ||
"""Return the name of the switch.""" | ||
return f"{DEFAULT_NAME}_{SWITCH}" | ||
|
||
@property | ||
def icon(self): | ||
"""Return the icon of this switch.""" | ||
return ICON | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the switch is on.""" | ||
return self.coordinator.data.get("title", "") == "foo" |