Skip to content

Commit

Permalink
switch: add basic switch to trigger security alarm mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vooon committed Feb 25, 2024
1 parent 6792d24 commit 3bc4357
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
7 changes: 6 additions & 1 deletion custom_components/myheat/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@
SENSOR = "sensor"
SWITCH = "switch"

PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS = [
Platform.CLIMATE,
Platform.SENSOR,
Platform.BINARY_SENSOR,
Platform.SWITCH,
]

# Defaults
DEFAULT_NAME = DOMAIN
Expand Down
48 changes: 23 additions & 25 deletions custom_components/myheat/switch.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,42 @@
"""Switch platform for MyHeat."""

from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DEFAULT_NAME
from .const import DOMAIN
from .const import ICON
from .const import SWITCH
from .entity import MhEntity


async def async_setup_entry(hass, entry, async_add_devices):
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_devices: AddEntitiesCallback,
):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_devices([MhBinarySwitch(coordinator, entry)])
async_add_devices([MhSecuritySwitch(coordinator, entry)])


class MhBinarySwitch(MhEntity, SwitchEntity):
class MhSecuritySwitch(MhEntity, SwitchEntity):
"""myheat switch class."""

async def async_turn_on(self, **kwargs): # 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
"""Turn off the switch."""
await self.coordinator.api.async_set_title("foo")
await self.coordinator.async_request_refresh()
_attr_icon = "mdi:security"

@property
def name(self):
"""Return the name of the switch."""
return f"{DEFAULT_NAME}_{SWITCH}"
def name(self) -> str:
return f"{self._mh_name} security alarm"

@property
def icon(self):
"""Return the icon of this switch."""
return ICON
def unique_id(self) -> str:
return f"{super().unique_id}security"

@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): # pylint: disable=unused-argument
await self.coordinator.api.async_set_security_mode(mode=True)
# NOTE: we cannot query the state, so we have to make some assumptions
self._attr_is_on = True

async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
await self.coordinator.api.async_set_security_mode(mode=False)
self._attr_is_on = False

0 comments on commit 3bc4357

Please sign in to comment.