Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def available(self) -> bool:
"""Return True if entity is available."""
return self._state in [SWITCHER_STATE_ON, SWITCHER_STATE_OFF]
def is_on(self) -> bool:
"""Return True if entity is on."""
return self._state == SWITCHER_STATE_ON
response: "SwitcherV2ControlResponseMSG" = None
async with SwitcherV2Api(
self.hass.loop,
self._device_data.ip_addr,
self._device_data.phone_id,
self._device_data.device_id,
self._device_data.device_password,
) as swapi:
response = await swapi.control_device(
COMMAND_ON if send_on else COMMAND_OFF
)
if response and response.successful:
self._self_initiated = True
self._state = SWITCHER_STATE_ON if send_on else SWITCHER_STATE_OFF
self.async_schedule_update_ha_state()
async def async_set_auto_off_service(service: ServiceCallType) -> None:
"""Use for handling setting device auto-off service calls."""
await _validate_edit_permission(
hass, service.context, service.data[CONF_ENTITY_ID]
)
async with SwitcherV2Api(
hass.loop, device_data.ip_addr, phone_id, device_id, device_password
) as swapi:
await swapi.set_auto_shutdown(service.data[CONF_AUTO_OFF])
async def _control_device(self, send_on: bool) -> None:
"""Turn the entity on or off."""
response: "SwitcherV2ControlResponseMSG" = None
async with SwitcherV2Api(
self.hass.loop,
self._device_data.ip_addr,
self._device_data.phone_id,
self._device_data.device_id,
self._device_data.device_password,
) as swapi:
response = await swapi.control_device(
COMMAND_ON if send_on else COMMAND_OFF
)
if response and response.successful:
self._self_initiated = True
self._state = SWITCHER_STATE_ON if send_on else SWITCHER_STATE_OFF
self.async_schedule_update_ha_state()
async def async_setup(hass: HomeAssistantType, config: Dict) -> bool:
"""Set up the switcher component."""
phone_id = config[DOMAIN][CONF_PHONE_ID]
device_id = config[DOMAIN][CONF_DEVICE_ID]
device_password = config[DOMAIN][CONF_DEVICE_PASSWORD]
v2bridge = SwitcherV2Bridge(hass.loop, phone_id, device_id, device_password)
await v2bridge.start()
async def async_stop_bridge(event: EventType) -> None:
"""On homeassistant stop, gracefully stop the bridge if running."""
await v2bridge.stop()
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_bridge)
try:
device_data = await wait_for(v2bridge.queue.get(), timeout=10.0)
except (Asyncio_TimeoutError, RuntimeError):
_LOGGER.exception("Failed to get response from device")
await v2bridge.stop()
return False
async def _control_device(self, send_on: bool) -> None:
"""Turn the entity on or off."""
response: "SwitcherV2ControlResponseMSG" = None
async with SwitcherV2Api(
self.hass.loop,
self._device_data.ip_addr,
self._device_data.phone_id,
self._device_data.device_id,
self._device_data.device_password,
) as swapi:
response = await swapi.control_device(
COMMAND_ON if send_on else COMMAND_OFF
)
if response and response.successful:
self._self_initiated = True
self._state = SWITCHER_STATE_ON if send_on else SWITCHER_STATE_OFF
self.async_schedule_update_ha_state()
def device_state_attributes(self) -> Dict:
"""Return the optional state attributes."""
attribs = {}
for prop, attr in DEVICE_PROPERTIES_TO_HA_ATTRIBUTES.items():
value = getattr(self._device_data, prop)
if value and value is not WAITING_TEXT:
attribs[attr] = value
return attribs