Hi fellow devs,
I’m working on my first dev effort with HA building a custom component. I’m trying to create the room
domain so I can define rooms in my house.
I want my integration to add a switch
entity and I was able to achieve that but whenever i turn_on
or turn_off
seems that nothing happens. The state never changes and the switch slider comes back to the previous position.
I’ve tested that is_on
works by hardcoding a value.
I’ve tried adding log entries on turn_on
and turn_off
but they don’t seem to work. Not sure if my implementation is wrong (if every switch must be added as a platform or anything). Any advice is heavily appreciated.
A little more background, custom_components/room/__init__.py
defines the ‘room’ DOMAIN.
My logs shows no errors.
Switch creation code:
def setup(hass, config):
switch_component = EntityComponent(_LOGGER, SWITCH_DOMAIN, hass)
switch_entities = []
switch_entities.append(
RoomPresenceHoldSwitch(hass, room_id, name)
)
switch_component.add_entities(switch_entities)
Switch code:
class RoomPresenceHoldSwitch(SwitchDevice, RestoreEntity):
def __init__(self, hass, room_id, name):
"""Initialize the presence hold switch."""
self._room_id = ENTITY_ID_FORMAT.format(room_id)
self.hass = hass
self._name = f"Presence Hold ({name})"
self._icon = 'mdi:car-brake-hold'
self._state = None
self._attributes = {}
@property
def name(self):
"""Return the name of the device if any."""
return self._name
@property
def device_state_attributes(self):
"""Return the attributes of the switch."""
return self._attributes
@property
def is_on(self):
"""Return true if presence hold is on."""
return (self._state == STATE_ON)
@property
def icon(self):
"""Return the icon to be used for this entity."""
if self._icon is not None:
return self._icon
return None
def turn_on(self, **kwargs):
"""Turn on presence hold."""
self._state = STATE_ON
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
"""Turn off presence hold."""
self._state = STATE_OFF
self.schedule_update_ha_state()