1 """Base class for pilight."""
3 import voluptuous
as vol
16 from .
import DOMAIN, EVENT, SERVICE_NAME
21 CONF_OFF_CODE_RECEIVE,
30 COMMAND_SCHEMA = vol.Schema(
32 vol.Optional(CONF_PROTOCOL): cv.string,
33 vol.Optional(CONF_ON): cv.positive_int,
34 vol.Optional(CONF_OFF): cv.positive_int,
35 vol.Optional(CONF_UNIT): cv.positive_int,
36 vol.Optional(CONF_UNITCODE): cv.positive_int,
37 vol.Optional(CONF_ID): vol.Any(cv.positive_int, cv.string),
38 vol.Optional(CONF_STATE): vol.Any(STATE_ON, STATE_OFF),
39 vol.Optional(CONF_SYSTEMCODE): cv.positive_int,
41 extra=vol.ALLOW_EXTRA,
44 RECEIVE_SCHEMA = COMMAND_SCHEMA.extend({vol.Optional(CONF_ECHO): cv.boolean})
46 SWITCHES_SCHEMA = vol.Schema(
48 vol.Required(CONF_ON_CODE): COMMAND_SCHEMA,
49 vol.Required(CONF_OFF_CODE): COMMAND_SCHEMA,
50 vol.Optional(CONF_NAME): cv.string,
51 vol.Optional(CONF_OFF_CODE_RECEIVE): vol.All(cv.ensure_list, [COMMAND_SCHEMA]),
52 vol.Optional(CONF_ON_CODE_RECEIVE): vol.All(cv.ensure_list, [COMMAND_SCHEMA]),
58 """Base class for pilight switches and lights."""
60 _attr_should_poll =
False
63 """Initialize a device."""
65 self.
_name_name = config.get(CONF_NAME, name)
67 self.
_code_on_code_on = config.get(CONF_ON_CODE)
70 code_on_receive = config.get(CONF_ON_CODE_RECEIVE, [])
71 code_off_receive = config.get(CONF_OFF_CODE_RECEIVE, [])
76 for code_list, conf
in (
81 echo = code.pop(CONF_ECHO,
True)
90 """Call when entity about to be added to hass."""
93 self.
_is_on_is_on = state.state == STATE_ON
94 self.
_brightness_brightness = state.attributes.get(
"brightness")
98 """Get the name of the switch."""
99 return self.
_name_name
103 """Return True if unable to access real state of the entity."""
108 """Return true if switch is on."""
112 """Check if received code by the pilight-daemon.
114 If the code matches the receive on/off codes of this switch the switch
115 state is changed accordingly.
122 if on_code.match(call.data):
123 on_code.run(switch=self, turn_on=
True)
128 if off_code.match(call.data):
129 off_code.run(switch=self, turn_on=
False)
132 def set_state(self, turn_on, send_code=True, dimlevel=None):
133 """Set the state of the switch.
135 This sets the state of the switch. If send_code is set to True, then
136 it will call the pilight.send service to actually send the codes
137 to the pilight daemon.
142 if dimlevel
is not None:
143 code.update({
"dimlevel": dimlevel})
145 self.
_hass_hass.services.call(DOMAIN, SERVICE_NAME, code, blocking=
True)
147 self.
_hass_hass.services.call(
148 DOMAIN, SERVICE_NAME, self.
_code_off_code_off, blocking=
True
151 self.
_is_on_is_on = turn_on
155 """Turn the switch on by calling pilight.send service with on code."""
159 """Turn the switch on by calling pilight.send service with off code."""
165 """Initialize the handle."""
170 """Test if the received code matches the configured values.
172 The received values have to be a subset of the configured options.
176 def run(self, switch, turn_on):
177 """Change the state of the switch."""
178 switch.set_state(turn_on=turn_on, send_code=self.
echoecho)
def turn_off(self, **kwargs)
def __init__(self, hass, name, config)
def async_added_to_hass(self)
def turn_on(self, **kwargs)
def set_state(self, turn_on, send_code=True, dimlevel=None)
def _handle_code(self, call)
def run(self, switch, turn_on)
def __init__(self, config, echo)
None schedule_update_ha_state(self, bool force_refresh=False)
State|None async_get_last_state(self)