def test_get_devices_failed(self): smart_system = SmartSystem(email="*****@*****.**", password="******") init_failed_mock(smart_system) smart_system.authenticate() location = Location(smart_system=smart_system) location.update_information(location_return) with pytest.raises(HTTPError): location.update_devices()
def setup_method(self, method): self.sm = SmartSystem(email="login", password="******", client_id="client_id") loc = Location(self.sm, location_fixture) self.sm.locations[loc.id] = loc device = Mower(self.sm, mower_fixture) loc.add_device(device)
def test_authenticate_failed_without_session(self): smart_system = SmartSystem(email="*****@*****.**", password="******") adapter = requests_mock.Adapter() adapter.register_uri("POST", "https://smart.gardena.com/sg-1/sessions", json={}, status_code=200) smart_system.request_session.mount("https://smart.gardena.com/", adapter) with pytest.raises(RuntimeError): smart_system.authenticate()
def test_authenticate(self): smart_system = SmartSystem(email="*****@*****.**", password="******") adapter = requests_mock.Adapter() adapter.register_uri( "POST", "https://smart.gardena.com/sg-1/sessions", json=authentication_return, status_code=200, ) smart_system.request_session.mount("https://smart.gardena.com/", adapter) smart_system.authenticate() assert smart_system.token == "7867e26c-05eb-4a60-bf30-7c3a1b4480aa" assert smart_system.user_id == "196ab891-a521-872c-ab1d-1685d1e77afc"
def test_authenticate_failed_without_token(self): smart_system = SmartSystem(email="*****@*****.**", password="******") adapter = requests_mock.Adapter() adapter.register_uri( "POST", "https://smart.gardena.com/sg-1/sessions", json={ "sessions": { "user_id": "196ab891-a521-872c-ab1d-1685d1e77afc" } }, status_code=200, ) smart_system.request_session.mount("https://smart.gardena.com/", adapter) with pytest.raises(RuntimeError): smart_system.authenticate()
def test_authenticate_failed_without_user_id(self): smart_system = SmartSystem(email="*****@*****.**", password="******") adapter = requests_mock.Adapter() adapter.register_uri( "POST", "https://smart.gardena.com/sg-1/sessions", json={ "sessions": { "token": "7867e26c-05eb-4a60-bf30-7c3a1b4480aa" } }, status_code=200, ) smart_system.request_session.mount("https://smart.gardena.com/", adapter) with pytest.raises(RuntimeError): smart_system.authenticate()
class SmartSystemTest(unittest.TestCase): def setup_method(self, method): self.sm = SmartSystem(email="login", password="******", client_id="client_id") loc = Location(self.sm, location_fixture) self.sm.locations[loc.id] = loc device = Mower(self.sm, mower_fixture) loc.add_device(device) def test_smart_system(self): self.sm.on_message(json.dumps(location_fixture)) assert len(self.sm.locations) == 1 def test_location_message(self): self.sm.locations[ "753aecac-4c46-470e-aa96-d92436f11e77"].id = "WRONG_VALUE" self.sm.locations[ "753aecac-4c46-470e-aa96-d92436f11e77"].name = "WRONG_VALUE" self.sm.on_message(json.dumps(location_fixture)) self.sm.on_message(json.dumps(mower_fixture["MOWER"][0])) location = self.sm.locations["753aecac-4c46-470e-aa96-d92436f11e77"] assert location.id == "753aecac-4c46-470e-aa96-d92436f11e77" assert location.name == "My Garden" assert len(location.devices) == 1 def test_device(self): self.sm.on_message(json.dumps(location_fixture)) self.sm.on_message(json.dumps(mower_fixture["MOWER"][0])) self.sm.locations["753aecac-4c46-470e-aa96-d92436f11e77"].devices[ "7859acbd-b23e-dabf-bec6-62c1453fc44c"].name == "WRONG_VALUE" device = self.sm.locations[ "753aecac-4c46-470e-aa96-d92436f11e77"].devices[ "7859acbd-b23e-dabf-bec6-62c1453fc44c"] assert device.id == "7859acbd-b23e-dabf-bec6-62c1453fc44c" assert device.name == "SILENO"
def test_call_update_locations_multiple_times(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() m_sessions = api_mock.register_sessions() m_locations = api_mock.register_locations() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() assert len(smart_system.locations) == 1 smart_system.update_locations() assert len(smart_system.locations) == 1 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. name == location_return["name"]) assert m_sessions.call_count == 1 assert m_locations.call_count == 2
def test_get_all_powers(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() api_mock.register_sessions() api_mock.register_two_locations() m_devices = api_mock.register_devices() m_devices_2 = api_mock.register_second_location_devices() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() smart_system.update_all_devices() assert len(smart_system.get_all_powers()) == 2 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. powers["c6e981e9-8ec6-438f-b400-c720d7f313c8"].name == "Power") assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. id == location_return_two["id"]) assert ( smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. powers["c6e981e9-8ec6-438f-b400-c720d7f313c9"].name == "Power 2") assert m_devices.call_count == 1 assert m_devices_2.call_count == 1
def test_get_all_mowers(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() api_mock.register_sessions() api_mock.register_two_locations() m_devices = api_mock.register_devices() m_devices_2 = api_mock.register_second_location_devices() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() smart_system.update_all_devices() assert len(smart_system.get_all_mowers()) == 2 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. mowers["e3c1b615-7351-25fc-a551-1908254a2b3e"].name == "Rosi") assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. id == location_return_two["id"]) assert ( smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. mowers["e3c1b615-7351-25fc-a551-1908254a2b3f"].name == "Rosi 2") assert m_devices.call_count == 1 assert m_devices_2.call_count == 1
def test_get_all_gateways(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() api_mock.register_sessions() api_mock.register_two_locations() m_devices = api_mock.register_devices() m_devices_2 = api_mock.register_second_location_devices() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() smart_system.update_all_devices() assert len(smart_system.get_all_gateways()) == 2 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. gateways["75cfc1f8-a20c-51d6-c5ea-1b5ecdde80c1"].name == "Gardena Zentrale") assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. id == location_return_two["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. gateways["75cfc1f8-a20c-51d6-c5ea-1b5ecdde80c2"].name == "Gardena Zentrale 2") assert m_devices.call_count == 1 assert m_devices_2.call_count == 1
def test_get_all_sensors(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() api_mock.register_sessions() api_mock.register_two_locations() m_devices = api_mock.register_devices() m_devices_2 = api_mock.register_second_location_devices() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() smart_system.update_all_devices() assert len(smart_system.get_all_sensors()) == 2 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert ( smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. sensors["a130596e-6627-4030-aea5-b6d2f24d0e03"].name == "Sensor") assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. id == location_return_two["id"]) assert ( smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. sensors["a130596e-6627-4030-aea5-b6d2f24d0e04"].name == "Sensor 2") assert m_devices.call_count == 1 assert m_devices_2.call_count == 1
def test_get_all_water_controls(self): smart_system = SmartSystem(email="*****@*****.**", password="******") api_mock = GardenaApiMock() api_mock.register_sessions() api_mock.register_two_locations() m_devices = api_mock.register_devices() m_devices_2 = api_mock.register_second_location_devices() api_mock.mount(smart_system) smart_system.authenticate() smart_system.update_locations() smart_system.update_all_devices() assert len(smart_system.get_all_water_controls()) == 2 assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. id == location_return["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1377"]. water_controls["d6259669-3241-488c-a88e-bcf3a07a58bf"].name == "Water Control") assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. id == location_return_two["id"]) assert (smart_system.locations["1c8b301f-22c8-423d-1b4d-ec25315d1378"]. water_controls["d6259669-3241-488c-a88e-bcf3a07a58c0"].name == "Water Control 2") assert m_devices.call_count == 1 assert m_devices_2.call_count == 1
def try_connection(email, password, client_id): _LOGGER.debug("Trying to connect to Gardena during setup") smart_system = SmartSystem(email=email, password=password, client_id=client_id) smart_system.authenticate() smart_system.update_locations() _LOGGER.debug("Successfully connected to Gardena during setup")
def setUp(self): """Prepare test fixtures.""" self.sm = SmartSystem(email="login", password="******", client_id="client_id")
def setup_method(self, method): self.sm = SmartSystem(email="login", password="******", client_id="client_id")
def test_update_locations_failed(self): smart_system = SmartSystem(email="*****@*****.**", password="******") init_failed_mock(smart_system) smart_system.authenticate() with pytest.raises(HTTPError): smart_system.update_locations()
def test_get_session(self): smart_system = SmartSystem(email="*****@*****.**", password="******") assert isinstance(smart_system.get_session(), requests.sessions.Session)
def get_smart_system_fixture(): return SmartSystem(email="*****@*****.**", password="******")
logging.info('===== Prepare MQTT Client =====') mqttclient = mqtt.Client(mqttclientid) mqttclient.username_pw_set(mqttuser, mqttpassword) mqttclient.on_connect = on_mqtt_connect mqttclient.on_disconnect = on_mqtt_disconnect mqttclient.on_message = on_mqtt_message mqttclient.will_set(f"{mqttprefix}/connected", "0", 0, True) mqttthread = Thread(target=mqttclient.loop_forever) mqttclientconnected = False logging.info('===== Prepare SmartSystem Client =====') logging.info(' - create') smart_system = SmartSystem(email=gardenauser, password=gardenapassword, client_id=gardenaapikey) logging.info(' - authenticate') smart_system.authenticate() logging.info(' - update location list') smart_system.update_locations() for location in smart_system.locations.values(): logging.info(f' - update device list for location : {location.name}') smart_system.update_devices(location) # add callbacks smart_system.add_ws_status_callback(on_ws_status_changed) for device in location.devices.values(): device.add_callback(on_device_update) smartsystemclientconnected = False
def test_init_exception_without_email(self): with pytest.raises(ValueError): SmartSystem(password="******")
def test_init(self): smart_system = SmartSystem(email="*****@*****.**", password="******") assert smart_system.email == "*****@*****.**" assert smart_system.password == "password"
def test_init_exception_without_password(self): with pytest.raises(ValueError): SmartSystem(email="*****@*****.**")