def testGetDeviceByEvent_validEvent_returnsExpectedDevice(self): zone = Zone('ff', [self.light]) eventInfo = EventInfo(ZoneEvent.MOTION, self.lightItem, zone, MockedZoneManager([zone]), self.getMockedEventDispatcher()) self.assertEqual(self.light, zone.getDeviceByEvent(eventInfo))
def testGetDeviceByEvent_invalidEvent_returnsNone(self): zone = Zone('ff', [self.light]) eventInfo = EventInfo(ZoneEvent.MOTION, self.motionSensorItem, zone, MockedZoneManager([zone]), self.getMockedEventDispatcher()) self.assertEqual(None, zone.getDeviceByEvent(eventInfo))
def testIsOccupied_switchIsOn_returnsTrue(self): zone = Zone('ff', [self.light, self.motionSensor]) self.light.turnOn(self.getMockedEventDispatcher()) (occupied, device) = zone.isOccupied() self.assertTrue(occupied) self.assertEqual(self.light, device)
def testIsOccupied_switchIsOnAndIgnoreLight_returnsFalse(self): zone = Zone('ff', [self.light, self.motionSensor]) self.light.turnOn(self.getMockedEventDispatcher()) (occupied, device) = zone.isOccupied([Light]) self.assertFalse(occupied) self.assertEqual(None, device)
def testOnSwitchedTurnedOff_validItemName_returnsTrue(self): zone = Zone('ff', [self.light]) isProcessed = zone.onSwitchTurnedOff(self.getMockedEventDispatcher(), self.lightItem, None) self.assertTrue(isProcessed)
def setUp(self): self.zone1 = Zone.createExternalZone('porch').addDevice(Door(ITEMS[0])) self.zone2 = Zone.createExternalZone('garage').addDevice(Door( ITEMS[1])) AlertManager._setTestMode(True) AlertManager.reset()
def testAddZone_validZone_zoneAdded(self): zone1 = Zone('ff') self.zm.addZone(zone1) self.assertEqual(1, len(self.zm.getZones())) zone2 = Zone('2f') self.zm.addZone(zone2) self.assertEqual(2, len(self.zm.getZones()))
def testOnAction_switchDisablesTriggeringByMotionSensor_returnsFalse(self): self.light1 = Light(self.lightItem1, 30, ILLUMINANCE_THRESHOLD_IN_LUX, True) self.zone1 = Zone('foyer', [self.light1, self.illuminanceSensor]) self.illuminanceSensorItem.setState( DecimalType(ILLUMINANCE_THRESHOLD_IN_LUX - 1)) self.assertFalse(self.turnOn())
def testIsOccupied_motionEventTriggeredButLightIsOff_returnsTrue(self): self.assertFalse(self.light.isOn()) zone = Zone('ff', [self.light, self.motionSensor, self.illuminanceSensor]) self.motionSensor._updateLastActivatedTimestamp() (occupied, device) = zone.isOccupied() self.assertTrue(occupied) self.assertEqual(self.motionSensor, device)
def testGetIlluminanceLevel_withSensor_returnsPositiveValue(self): self.illuminanceSensorItem.setState( DecimalType(ILLUMINANCE_THRESHOLD_IN_LUX)) zone = Zone('ff', [ self.lightWithIlluminance, self.motionSensor, self.illuminanceSensor ]) self.assertEqual(ILLUMINANCE_THRESHOLD_IN_LUX, zone.getIlluminanceLevel())
def _normalizeNeighbors(self, zone): ''' If a zone has the same neighbor with more than one OPEN_SPACE type, remove the generic one NeighborType.OPEN_SPACE :rtype: Zone new object ''' zoneIdToType = {} for neighbor in zone.getNeighbors(): zoneId = neighbor.getZoneId() if zoneId in zoneIdToType: types = zoneIdToType[zoneId] else: types = [] zoneIdToType[zoneId] = types types.append(neighbor.getType()) for types in zoneIdToType.values(): if NeighborType.OPEN_SPACE_MASTER in types \ or NeighborType.OPEN_SPACE_SLAVE in types: if NeighborType.OPEN_SPACE in types: types.remove(NeighborType.OPEN_SPACE) zone = Zone(zone.getName(), zone.getDevices(), zone.getLevel(), [], {}, zone.isExternal(), zone.getDisplayIcon(), zone.getDisplayOrder()) for zoneId in zoneIdToType.keys(): for type in zoneIdToType[zoneId]: zone = zone.addNeighbor(Neighbor(zoneId, type)) return zone
def testOnMotionSensorTurnedOn_notLightOnTime_returnsFalse(self): self.astroSensorItem.setState(StringType('MORNING')) zone = Zone('ff', [self.light, self.astroSensor]) zone = zone.addAction(TurnOnSwitch()) isProcessed = zone.dispatchEvent(ZoneEvent.MOTION, self.getMockedEventDispatcher(), self.motionSensor.getItem(), None, True) self.assertFalse(isProcessed)
def testGetDevicesByType_variousScenarios_returnsCorrectList(self): zone1 = Zone('ff').addDevice(self.light) zone2 = Zone('sf').addDevice(self.fan) self.zm.addZone(zone1) self.zm.addZone(zone2) self.assertEqual(2, len(self.zm.getZones())) self.assertEqual(1, len(self.zm.getDevicesByType(Light))) self.assertEqual(2, len(self.zm.getDevicesByType(Switch))) self.assertEqual(0, len(self.zm.getDevicesByType(AstroSensor)))
def testOnMotionSensorTurnedOn_validItemNameNoIlluminanceSensorNoAstroSensor_returnsFalse( self): self.assertFalse(self.light.isOn()) zone = Zone('ff', [self.light, self.motionSensor]) zone = zone.addAction(TurnOnSwitch()) isProcessed = zone.dispatchEvent(ZoneEvent.MOTION, self.getMockedEventDispatcher(), self.motionSensor.getItem(), MockedZoneManager([zone]), True) self.assertFalse(isProcessed)
def testContainingZone_validDevice_returnsCorrectZone(self): zone1 = Zone('ff').addDevice(self.light) zone2 = Zone('sf').addDevice(self.fan) self.zm.addZone(zone1) self.zm.addZone(zone2) self.assertEqual( zone1, self.zm._createImmutableInstance().getContainingZone(self.light)) self.assertEqual( zone2, self.zm._createImmutableInstance().getContainingZone(self.fan))
def testOnMotionSensorTurnedOn_withApplicableZone_returnsTrue(self): self.assertFalse(self.light.isOn()) self.illuminanceSensorItem.setState( DecimalType(ILLUMINANCE_THRESHOLD_IN_LUX - 1)) zone = Zone('ff', [self.light, self.motionSensor, self.illuminanceSensor]) zone = zone.addAction(TurnOnSwitch()) self.zm.addZone(zone) self.assertTrue( self.zm.dispatchEvent(ZoneEvent.MOTION, scope.events, self.motionSensor.getItem()))
def testOnMotionSensorTurnedOn_lightOnTime_turnsOnLight(self): self.assertFalse(self.light.isOn()) self.astroSensorItem.setState(StringType( AstroSensor.LIGHT_ON_TIMES[0])) zone = Zone('ff', [self.light, self.motionSensor, self.astroSensor]) zone = zone.addAction(TurnOnSwitch()) isProcessed = zone.dispatchEvent(ZoneEvent.MOTION, self.getMockedEventDispatcher(), self.motionSensor.getItem(), MockedZoneManager([zone]), True) self.assertTrue(isProcessed) self.assertTrue(self.light.isOn())
def setUp(self): super(TurnOffAdjacentZonesTest, self).setUp() [self.lightItem1, self.lightItem2, self.lightItem3, self.fanItem] = self.getItems() self.light1 = Light(self.lightItem1, 5) self.light2 = Light(self.lightItem2, 5) self.light3 = Light(self.lightItem3, 5, ILLUMINANCE_THRESHOLD_IN_LUX, False, "0-23:59") # always stay on self.fan = Fan(self.fanItem, 5) self.washroom = Zone('washroom', [self.light1]) self.shower = Zone('shower', [self.fan]) self.lobby = Zone('lobby', [self.light2]) self.foyer = Zone('foyer', [self.light3]) self.zoneManager = MockedZoneManager( [self.washroom, self.shower, self.lobby, self.foyer]) self.lobby = self.lobby.addNeighbor( Neighbor(self.foyer.getId(), NeighborType.OPEN_SPACE)) self.washroom = self.washroom.addNeighbor( Neighbor(self.lobby.getId(), NeighborType.OPEN_SPACE)) self.washroom = self.washroom.addNeighbor( Neighbor(self.shower.getId(), NeighborType.OPEN_SPACE))
def testRemoveZone_validZone_zoneRemoved(self): zone1 = Zone('ff') self.zm.addZone(zone1) zone2 = Zone('2f') self.zm.addZone(zone2) self.assertEqual(2, len(self.zm.getZones())) self.zm.removeZone(zone1) self.assertEqual(1, len(self.zm.getZones())) self.zm.removeZone(zone2) self.assertEqual(0, len(self.zm.getZones()))
def testContainingZone_invalidDevice_returnsNone(self): zone1 = Zone('ff').addDevice(self.light) self.zm.addZone(zone1) self.assertEqual( None, self.zm._createImmutableInstance().getContainingZone(self.fan))
def testOnMotionSensorTurnedOn_withNonApplicableZone_returnsFalse(self): zone = Zone('ff', [self.light, self.motionSensor]) self.zm.addZone(zone) self.assertFalse( self.zm.dispatchEvent(ZoneEvent.MOTION, scope.events, PE.createStringItem(INVALID_ITEM_NAME)))
def setUp(self): self.action = AlertOnHumidityOutOfRange(35, 50, 3) self.zone1 = Zone('great room', [], Level.FIRST_FLOOR).addDevice(HumiditySensor(ITEMS[0])) AlertManager._setTestMode(True) AlertManager.reset() cast_manager._setTestMode(True)
def setUp(self): self.action = AlertOnTemperatureOutOfRange(35, 50, 3) self.zone1 = Zone('great room').addDevice(TemperatureSensor(ITEMS[0])) AlertManager._setTestMode(True) AlertManager.reset() cast_manager._setTestMode(True)
def testOnSwitchTurnedOff_withNonApplicableZone_returnsFalse(self): zone = Zone('ff', [self.light, self.motionSensor]) self.zm.addZone(zone) self.assertFalse( self.zm.onSwitchTurnedOff(scope.events, PE.createStringItem(INVALID_ITEM_NAME)))
def testOnAction_zoneIsExternal_returnsFalse(self): zone = Zone.createExternalZone('porch').addDevice( TemperatureSensor(ITEMS[0])) eventInfo = EventInfo(ZoneEvent.TEMPERATURE_CHANGED, ITEMS[0], zone, None, self.getMockedEventDispatcher()) value = AlertOnTemperatureOutOfRange().onAction(eventInfo) self.assertFalse(value)
def _createZone(self, itemName, zoneName): ''' :return: the zone associated with the itemName :rtype: Zone ''' levelMeta = MetadataRegistry.get(MetadataKey('level', itemName)) if None == levelMeta: raise ValueError( 'The zone level must be specified as BM, FF, SF, or TF') level = self._getZoneLevel(levelMeta.value) externalMeta = MetadataRegistry.get(MetadataKey('external', itemName)) external = None != externalMeta and "true" == externalMeta.value.lower( ) displayIconMeta = MetadataRegistry.get( MetadataKey('displayIcon', itemName)) if None != displayIconMeta: displayIcon = displayIconMeta.value else: displayIcon = None displayOrderMeta = MetadataRegistry.get( MetadataKey('displayOrder', itemName)) if None != displayOrderMeta: displayOrder = int(displayOrderMeta.value) else: displayOrder = 9999 zone = Zone(zoneName, [], level, [], {}, external, displayIcon, displayOrder) neighbors = [] openSpaceMeta = MetadataRegistry.get( MetadataKey('openSpaceNeighbors', itemName)) if None != openSpaceMeta: for neighborId in openSpaceMeta.value.split(','): neighborId = neighborId.strip() # Temporary replacement to align with HABapp zone-apis code. neighborId = neighborId.replace('FF_', '1_') neighborId = neighborId.replace('SF_', '2_') neighbors.append( [zone.getId(), neighborId, NeighborType.OPEN_SPACE]) return [zone, neighbors]
def testStr_noParam_returnsNonEmptyString(self): zone = Zone('ff', [ self.light, self.motionSensor, self.astroSensor, self.illuminanceSensor, self.dimmer, self.fan ]) info = str(zone) self.assertTrue(len(info) > 0)
def testOnAction_switchOffEvent_pauseStreamAndReturnsTrue(self): zone1 = Zone('shower').addDevice(self.sink) eventInfo = EventInfo(ZoneEvent.SWITCH_TURNED_OFF, ITEMS[0], zone1, None, scope.events) value = self.action.onAction(eventInfo) self.assertTrue(value) self.assertEqual('pause', self.sink._getLastTestCommand())
def testOnMotionSensorTurnedOn_illuminanceAboveThreshold_returnsFalse( self): self.assertFalse(self.light.isOn()) self.illuminanceSensorItem.setState( DecimalType(ILLUMINANCE_THRESHOLD_IN_LUX + 1)) zone = Zone('ff', [ self.lightWithIlluminance, self.motionSensor, self.illuminanceSensor ]) zone = zone.addAction(TurnOnSwitch()) isProcessed = zone.dispatchEvent(ZoneEvent.MOTION, self.getMockedEventDispatcher(), self.motionSensor.getItem(), MockedZoneManager([zone]), True) self.assertFalse(isProcessed) self.assertFalse(self.light.isOn())
def setUp(self): super(ArmAfterFrontDoorClosedTest, self).setUp() self.alarmPartition = AlarmPartition(ITEMS[2], ITEMS[3]) self.externalMotionSensor = MotionSensor(ITEMS[4]) self.internalMotionSensor = MotionSensor(ITEMS[5]) self.zone1 = Zone.createExternalZone('porch') \ .addDevice(Door(ITEMS[0])) \ .addDevice(self.externalMotionSensor) self.zone2 = Zone('foyer', [self.internalMotionSensor, self.alarmPartition]) self.mockZoneManager= MockZoneManager(self.alarmPartition, [self.zone1, self.zone2]) AlertManager._setTestMode(True) AlertManager.reset()