Skip to content

Commit 3bc4357

Browse files
committed
switch: add basic switch to trigger security alarm mode
1 parent 6792d24 commit 3bc4357

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

custom_components/myheat/const.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
SENSOR = "sensor"
2727
SWITCH = "switch"
2828

29-
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.BINARY_SENSOR]
29+
PLATFORMS = [
30+
Platform.CLIMATE,
31+
Platform.SENSOR,
32+
Platform.BINARY_SENSOR,
33+
Platform.SWITCH,
34+
]
3035

3136
# Defaults
3237
DEFAULT_NAME = DOMAIN

custom_components/myheat/switch.py

+23-25
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
"""Switch platform for MyHeat."""
22

33
from homeassistant.components.switch import SwitchEntity
4+
from homeassistant.config_entries import ConfigEntry
5+
from homeassistant.core import HomeAssistant
6+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
47

5-
from .const import DEFAULT_NAME
68
from .const import DOMAIN
7-
from .const import ICON
8-
from .const import SWITCH
99
from .entity import MhEntity
1010

1111

12-
async def async_setup_entry(hass, entry, async_add_devices):
12+
async def async_setup_entry(
13+
hass: HomeAssistant,
14+
entry: ConfigEntry,
15+
async_add_devices: AddEntitiesCallback,
16+
):
1317
"""Setup sensor platform."""
1418
coordinator = hass.data[DOMAIN][entry.entry_id]
15-
async_add_devices([MhBinarySwitch(coordinator, entry)])
19+
async_add_devices([MhSecuritySwitch(coordinator, entry)])
1620

1721

18-
class MhBinarySwitch(MhEntity, SwitchEntity):
22+
class MhSecuritySwitch(MhEntity, SwitchEntity):
1923
"""myheat switch class."""
2024

21-
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
22-
"""Turn on the switch."""
23-
await self.coordinator.api.async_set_title("bar")
24-
await self.coordinator.async_request_refresh()
25-
26-
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
27-
"""Turn off the switch."""
28-
await self.coordinator.api.async_set_title("foo")
29-
await self.coordinator.async_request_refresh()
25+
_attr_icon = "mdi:security"
3026

3127
@property
32-
def name(self):
33-
"""Return the name of the switch."""
34-
return f"{DEFAULT_NAME}_{SWITCH}"
28+
def name(self) -> str:
29+
return f"{self._mh_name} security alarm"
3530

3631
@property
37-
def icon(self):
38-
"""Return the icon of this switch."""
39-
return ICON
32+
def unique_id(self) -> str:
33+
return f"{super().unique_id}security"
4034

41-
@property
42-
def is_on(self):
43-
"""Return true if the switch is on."""
44-
return self.coordinator.data.get("title", "") == "foo"
35+
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
36+
await self.coordinator.api.async_set_security_mode(mode=True)
37+
# NOTE: we cannot query the state, so we have to make some assumptions
38+
self._attr_is_on = True
39+
40+
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
41+
await self.coordinator.api.async_set_security_mode(mode=False)
42+
self._attr_is_on = False

0 commit comments

Comments
 (0)