1 """Support for Wireless Sensor Tags."""
5 from requests.exceptions
import ConnectTimeout, HTTPError
6 import voluptuous
as vol
7 from wirelesstagpy
import WirelessTags
8 from wirelesstagpy.exceptions
import WirelessTagsException
17 from .const
import DOMAIN, SIGNAL_BINARY_EVENT_UPDATE, SIGNAL_TAG_UPDATE
19 _LOGGER = logging.getLogger(__name__)
21 NOTIFICATION_ID =
"wirelesstag_notification"
22 NOTIFICATION_TITLE =
"Wireless Sensor Tag Setup"
24 DEFAULT_ENTITY_NAMESPACE =
"wirelesstag"
26 CONFIG_SCHEMA = vol.Schema(
30 vol.Required(CONF_USERNAME): cv.string,
31 vol.Required(CONF_PASSWORD): cv.string,
35 extra=vol.ALLOW_EXTRA,
40 """Principal object to manage all registered in HA tags."""
43 """Designated initializer for wirelesstags platform."""
50 """Load tags from remote server."""
54 def arm(self, switch):
55 """Arm entity sensor monitoring."""
56 func_name = f
"arm_{switch.entity_description.key}"
57 if (arm_func := getattr(self.
apiapi, func_name))
is not None:
58 arm_func(switch.tag_id, switch.tag_manager_mac)
61 """Disarm entity sensor monitoring."""
62 func_name = f
"disarm_{switch.entity_description.key}"
63 if (disarm_func := getattr(self.
apiapi, func_name))
is not None:
64 disarm_func(switch.tag_id, switch.tag_manager_mac)
67 """Start monitoring push events."""
69 def push_callback(tags_spec, event_spec):
70 """Handle push update."""
72 "Push notification arrived: %s, events: %s", tags_spec, event_spec
74 for uuid, tag
in tags_spec.items():
77 mac = tag.tag_manager_mac
78 _LOGGER.debug(
"Push notification for tag update arrived: %s", tag)
80 self.
hasshass, SIGNAL_TAG_UPDATE.format(tag_id, mac), tag
82 if uuid
in event_spec:
83 events = event_spec[uuid]
86 "Push notification for binary event arrived: %s", event
90 SIGNAL_BINARY_EVENT_UPDATE.format(
91 tag_id, event.type, mac
95 except Exception
as ex:
97 "Unable to handle tag update: %s error: %s",
105 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
106 """Set up the Wireless Sensor Tag component."""
107 conf = config[DOMAIN]
108 username = conf.get(CONF_USERNAME)
109 password = conf.get(CONF_PASSWORD)
112 wirelesstags = WirelessTags(username=username, password=password)
116 platform.start_monitoring()
117 hass.data[DOMAIN] = platform
118 except (ConnectTimeout, HTTPError, WirelessTagsException)
as ex:
119 _LOGGER.error(
"Unable to connect to wirelesstag.net service: %s",
str(ex))
120 persistent_notification.create(
122 f
"Error: {ex}<br />Please restart hass after fixing this.",
123 title=NOTIFICATION_TITLE,
124 notification_id=NOTIFICATION_ID,
bool setup(HomeAssistant hass, ConfigType config)
None dispatcher_send(HomeAssistant hass, str signal, *Any args)