mysql = pymysql.connect(host=os.getenv('MYSQL_HOST', private.mysql_host), user=os.getenv('MYSQL_USER', private.mysql_user), password=os.getenv('MYSQL_PASSWORD', private.mysql_password), db=os.getenv('MYSQL_DATABASE', private.mysql_database)) api = Tado(private.username, private.password) weather = api.getWeather() outsideTemperature = weather["outsideTemperature"]["celsius"] zones = {zone["name"]: zone["id"] for zone in api.getZones()} for name in private.zones: zone = zones[name] state = api.getState(zone) # create new table with warnings.catch_warnings(), mysql.cursor() as cursor: # temperatures are < 100 with up to two digits past the comma --> DECIMAL(4,2) # percentages are <= 100 with only one digit past the comma --> DECIMAL(4,1) sql = """ CREATE TABLE IF NOT EXISTS `{}` ( `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `outsideTemperature` DECIMAL(4,2), `setpoint` DECIMAL(4,2), `temperature` DECIMAL(4,2), `humidity` DECIMAL(4,1), `heatingpower` DECIMAL(4,1), PRIMARY KEY (`timestamp`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
class TadoConnector: """An object to store the Tado data.""" def __init__(self, hass, username, password): """Initialize Tado Connector.""" self.hass = hass self._username = username self._password = password self.tado = None self.zones = None self.devices = None self.data = { "zone": {}, "device": {}, } def setup(self): """Connect to Tado and fetch the zones.""" try: self.tado = Tado(self._username, self._password) except (RuntimeError, urllib.error.HTTPError) as exc: _LOGGER.error("Unable to connect: %s", exc) return False self.tado.setDebugging(True) # Load zones and devices self.zones = self.tado.getZones() self.devices = self.tado.getMe()["homes"] return True @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): """Update the registered zones.""" for zone in self.zones: self.update_sensor("zone", zone["id"]) for device in self.devices: self.update_sensor("device", device["id"]) def update_sensor(self, sensor_type, sensor): """Update the internal data from Tado.""" _LOGGER.debug("Updating %s %s", sensor_type, sensor) try: if sensor_type == "zone": data = self.tado.getState(sensor) elif sensor_type == "device": data = self.tado.getDevices()[0] else: _LOGGER.debug("Unknown sensor: %s", sensor_type) return except RuntimeError: _LOGGER.error( "Unable to connect to Tado while updating %s %s", sensor_type, sensor, ) return self.data[sensor_type][sensor] = data _LOGGER.debug("Dispatching update to %s %s: %s", sensor_type, sensor, data) dispatcher_send( self.hass, SIGNAL_TADO_UPDATE_RECEIVED.format(sensor_type, sensor) ) def get_capabilities(self, zone_id): """Return the capabilities of the devices.""" return self.tado.getCapabilities(zone_id) def reset_zone_overlay(self, zone_id): """Reset the zone back to the default operation.""" self.tado.resetZoneOverlay(zone_id) self.update_sensor("zone", zone_id) def set_zone_overlay( self, zone_id, overlay_mode, temperature=None, duration=None, device_type="HEATING", mode=None, ): """Set a zone overlay.""" _LOGGER.debug( "Set overlay for zone %s: mode=%s, temp=%s, duration=%s, type=%s, mode=%s", zone_id, overlay_mode, temperature, duration, device_type, mode, ) try: self.tado.setZoneOverlay( zone_id, overlay_mode, temperature, duration, device_type, "ON", mode ) except urllib.error.HTTPError as exc: _LOGGER.error("Could not set zone overlay: %s", exc.read()) self.update_sensor("zone", zone_id) def set_zone_off(self, zone_id, overlay_mode, device_type="HEATING"): """Set a zone to off.""" try: self.tado.setZoneOverlay( zone_id, overlay_mode, None, None, device_type, "OFF" ) except urllib.error.HTTPError as exc: _LOGGER.error("Could not set zone overlay: %s", exc.read()) self.update_sensor("zone", zone_id)
def get_state(args): t = log_in(args.email, args.password) zone = tado_client.getState(t, int(args.zone)) print(zone)