1 """Support for SCSGate switches."""
3 from __future__
import annotations
8 from scsgate.messages
import ScenarioTriggeredMessage, StateMessage
9 from scsgate.tasks
import ToggleStatusTask
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
22 from .
import CONF_SCS_ID, DOMAIN, SCSGATE_SCHEMA
24 ATTR_SCENARIO_ID =
"scenario_id"
26 CONF_TRADITIONAL =
"traditional"
27 CONF_SCENARIO =
"scenario"
29 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
30 {vol.Required(CONF_DEVICES): cv.schema_with_slug_keys(SCSGATE_SCHEMA)}
37 add_entities: AddEntitiesCallback,
38 discovery_info: DiscoveryInfoType |
None =
None,
40 """Set up the SCSGate switches."""
41 logger = logging.getLogger(__name__)
42 scsgate = hass.data[DOMAIN]
48 add_entities_callback=add_entities,
55 """Add traditional SCSGate switches."""
56 traditional = config.get(CONF_TRADITIONAL)
60 for entity_info
in traditional.values():
61 if entity_info[CONF_SCS_ID]
in scsgate.devices:
64 name = entity_info[CONF_NAME]
65 scs_id = entity_info[CONF_SCS_ID]
67 logger.info(
"Adding %s scsgate.traditional_switch", name)
70 name=name, scs_id=scs_id, logger=logger, scsgate=scsgate
72 switches.append(switch)
74 add_entities_callback(switches)
75 scsgate.add_devices_to_register(switches)
79 """Add only SCSGate scenario switches."""
80 if scenario := config.get(CONF_SCENARIO):
81 for entity_info
in scenario.values():
82 if entity_info[CONF_SCS_ID]
in scsgate.devices:
85 name = entity_info[CONF_NAME]
86 scs_id = entity_info[CONF_SCS_ID]
88 logger.info(
"Adding %s scsgate.scenario_switch", name)
91 name=name, scs_id=scs_id, logger=logger, hass=hass
93 scsgate.add_device(switch)
97 """Representation of a SCSGate switch."""
99 _attr_should_poll =
False
102 """Initialize the switch."""
111 """Return the SCS ID."""
116 """Return the name of the device if any."""
117 return self.
_name_name
121 """Return true if switch is on."""
125 """Turn the device on."""
127 self.
_scsgate_scsgate.append_task(ToggleStatusTask(target=self.
_scs_id_scs_id, toggled=
True))
133 """Turn the device off."""
135 self.
_scsgate_scsgate.append_task(ToggleStatusTask(target=self.
_scs_id_scs_id, toggled=
False))
141 """Handle a SCSGate message related with this switch."""
142 if self.
_toggled_toggled == message.toggled:
144 "Switch %s, ignoring message %s because state already active",
151 self.
_toggled_toggled = message.toggled
158 self.
hasshass.bus.fire(
159 "button_pressed", {ATTR_ENTITY_ID: self.
_scs_id_scs_id, ATTR_STATE: command}
164 """Provides a SCSGate scenario switch.
166 This switch is always in an 'off" state, when toggled it's used to trigger
171 """Initialize the scenario."""
179 """Return the SCS ID."""
184 """Return the name of the device if any."""
185 return self.
_name_name
188 """Handle a SCSGate message related with this switch."""
190 if isinstance(message, StateMessage):
191 scenario_id = message.bytes[4]
192 elif isinstance(message, ScenarioTriggeredMessage):
193 scenario_id = message.scenario
196 "Scenario switch: received unknown message %s", message
200 self.
_hass_hass.bus.fire(
201 "scenario_switch_triggered",
202 {ATTR_ENTITY_ID:
int(self.
_scs_id_scs_id), ATTR_SCENARIO_ID:
int(scenario_id, 16)},
def __init__(self, scs_id, name, logger, hass)
def process_event(self, message)
def __init__(self, scs_id, name, logger, scsgate)
def process_event(self, message)
None turn_on(self, **Any kwargs)
None turn_off(self, **Any kwargs)
None schedule_update_ha_state(self, bool force_refresh=False)
def _setup_traditional_switches(logger, config, scsgate, add_entities_callback)
def _setup_scenario_switches(logger, config, scsgate, hass)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)