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 2 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
14 changes: 12 additions & 2 deletions custom_components/rpi_rf/switch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Support for a switch using a 433MHz module via GPIO on a Raspberry Pi."""
from __future__ import annotations
from email.policy import default
markvader marked this conversation as resolved.
Show resolved Hide resolved
from enum import unique
markvader marked this conversation as resolved.
Show resolved Hide resolved

import importlib
import logging
Expand All @@ -10,6 +12,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 +43,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 +62,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 +75,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 +91,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 +101,7 @@ class RPiRFSwitch(SwitchEntity):
def __init__(
self,
name,
unique_id,
rfdevice,
lock,
protocol,
Expand All @@ -106,6 +113,7 @@ def __init__(
):
"""Initialize the switch."""
self._name = name
self._attr_unique_id = unique_id if unique_id else "{}_{}_{}".format(name, code_on, code_off)
self._state = False
self._rfdevice = rfdevice
self._lock = lock
Expand All @@ -116,6 +124,8 @@ def __init__(
self._code_off = code_off
self._rfdevice.tx_repeat = signal_repetitions



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