Skip to content

Commit

Permalink
USe SwitchEntityDescription in switch
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Feb 11, 2023
1 parent b192db6 commit ac1caeb
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions custom_components/integration_blueprint/switch.py
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"

0 comments on commit ac1caeb

Please sign in to comment.