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 to entity #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 17 additions & 6 deletions custom_components/denkovi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Denkovi switches."""
resource = config.get(CONF_RESOURCE)
password = config.get(CONF_PASSWORD)
name = config.get(CONF_NAME)

try:
denkoviModule = DenkoviModule(resource, password)
denkoviModule = DenkoviModule(resource, password, name)
except DenkoviException as e:
_LOGGER.error(str(e))
return False
Expand All @@ -56,11 +57,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):

class DenkoviModule():

def __init__(self, resource, password):
def __init__(self, resource, password, name):
self._resource = resource
self._password = password
self._name = name
self.update()

@property
def name(self):
return self._name

def turn_on_or_off(self, relay, payload):
try:
self._response = requests.get('{}/current_state.json?pw={}&Relay{}={}'.format(self._resource, self._password, relay, payload),
Expand Down Expand Up @@ -94,13 +100,19 @@ def update(self):
class DenkoviSwitchBase(SwitchEntity):
"""Representation of an Denkovi switch."""

def __init__(self, denkoviModule, name):
def __init__(self, denkoviModule, name, relay):
"""Initialize the switch."""
self._denkoviModule = denkoviModule
self._relay = relay
self._name = name
self._state = None
self._available = True

@property
def unique_id(self):
"""Return the unique ID."""
return f"{self._denkoviModule.name}_{self._relay}"

@property
def name(self):
"""Return the name of the switch."""
Expand All @@ -122,8 +134,7 @@ class DenkoviSwitchRelay(DenkoviSwitchBase):

def __init__(self, denkoviModule, name, relay, invert):
"""Initialize the switch."""
super().__init__(denkoviModule, name)
self._relay = relay
super().__init__(denkoviModule, name, relay)
self._invert = invert
self.update()

Expand Down Expand Up @@ -160,7 +171,7 @@ def update(self):
except DenkoviException as e:
_LOGGER.error("Error updating state for relay %s, %s", str(self._relay), str(e))
self._available = False

class DenkoviException(Exception):
pass

Expand Down