Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unique_id functionality. Required for further customization in HA UI #48

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions custom_components/rpi_rf/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.const import (
CONF_NAME,
CONF_UNIQUE_ID,
CONF_PROTOCOL,
CONF_SWITCHES,
EVENT_HOMEASSISTANT_STOP,
Expand Down Expand Up @@ -40,6 +41,7 @@
vol.Optional(CONF_SIGNAL_REPETITIONS, default=DEFAULT_SIGNAL_REPETITIONS): cv.positive_int,
vol.Optional(CONF_PROTOCOL, default=DEFAULT_PROTOCOL): cv.positive_int,
vol.Optional(CONF_LENGTH, default=DEFAULT_LENGTH): cv.positive_int,
vol.Optional(CONF_UNIQUE_ID): cv.string,
}
)

Expand All @@ -58,7 +60,7 @@ def setup_platform(
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Find and return switches controlled by a generic RF device via GPIO."""

rpi_rf = importlib.import_module("rpi_rf")

gpio = config[CONF_GPIO]
Expand All @@ -71,6 +73,7 @@ def setup_platform(
devices.append(
RPiRFSwitch(
properties.get(CONF_NAME, dev_name),
properties.get(CONF_UNIQUE_ID),
rfdevice,
rfdevice_lock,
properties.get(CONF_PROTOCOL),
Expand All @@ -86,7 +89,8 @@ def setup_platform(

add_entities(devices)

hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: rfdevice.cleanup())
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
lambda event: rfdevice.cleanup())


class RPiRFSwitch(SwitchEntity):
Expand All @@ -95,6 +99,7 @@ class RPiRFSwitch(SwitchEntity):
def __init__(
self,
name,
unique_id,
rfdevice,
lock,
protocol,
Expand All @@ -106,6 +111,7 @@ def __init__(
):
"""Initialize the switch."""
self._name = name
self._attr_unique_id = unique_id if unique_id else "{}_{}".format(code_on, code_off)
self._state = False
self._rfdevice = rfdevice
self._lock = lock
Expand All @@ -116,6 +122,8 @@ def __init__(
self._code_off = code_off
self._rfdevice.tx_repeat = signal_repetitions



@property
def should_poll(self):
"""No polling needed."""
Expand Down